Im getting the error ""First argument must be a string" and i cant understand why, in the following line:
instring = fread(s,s.BytesAvailable,'uint8');
invalues = sscanf(instring,'%f');
Isn't my "instring" the first string?
If you need the rest of my code i will provide it.
All im trying to do is transfer 6 continuous values from my arduino serially (though 1 serial port) into my matlab to be displayed onto a graph.
Any help is highly appreciated!
What does the command
class(instring)
show to you?
--Nasser
PLease include the message you are replying to, so it is easy to
comment.
you said:
" Isn't my "instring" the first string?"
And I simply asked if you find the type of 'instring' by
typing, in matlab the following:
class(instring)
this will tell you what matlab sees as the type of instring.
--Nasser
I guess you need a stronger hint. See this line:
instring = fread(s,s.BytesAvailable,'uint8');
Now, what do you think the 'uint8' does? Do you think it maybe forces
instring to be of type uint8 instead of character?
I also have questions about your s. If s is the fileID gotten by
calling fopen(), then why do you think that it is a structure with a
BytesAvailable member? I'm surprised that that line actually even
works at all.
When i typed "class(instring)" it gave me
"ans = double"
What does that mean? Two arrays?
I removed the "uint8" as my program is connected through a serial port (forgot to mention it here).
Now the line goes: "instring = fread(s,s.BytesAvailable);"
But i get the error "SIZE must be greater than 0.". So my matlab isnt receiving any bytes?
And why do you need sscanf to get the values when fread gives them to
you directly? It's just not needed. If you know what's there, you
can tell it to return what's there. Or just use the '*source' option
of fread.
I previously didnt have fread in my code before, i was using it now to try read my error, but i was going to remove it again as i dont need it.
Here's my entire code, maybe u might understand why im getting an eror because i was so lost! (bottom part of code is where my errors occur!)
SerialPort='com9'; %serial port
MaxDeviation = 3; %Maximum Allowable Change from one value to next
TimeInterval=0.2; %time interval between each input.
loop=120; %count values
%%Set up the serial port object
s = serial(SerialPort)
fopen(s);
time =now;
voltage = 0;
%% Set up the figure
figureHandle = figure('NumberTitle','off',...
'Name','Voltage Characteristics',...
'Color',[0 0 0],'Visible','off');
% Set axes
axesHandle = axes('Parent',figureHandle,...
'YGrid','on',...
'YColor',[0.9725 0.9725 0.9725],...
'XGrid','on',...
'XColor',[0.9725 0.9725 0.9725],...
'Color',[0 0 0]);
hold on;
plotHandle = plot(axesHandle,time,voltage,'Marker','.','LineWidth',1,'Color',[0 1 0]);
xlim(axesHandle,[min(time) max(time+0.001)]);
% Create xlabel
xlabel('Time','FontWeight','bold','FontSize',14,'Color',[1 1 0]);
% Create ylabel
ylabel('Voltage in V','FontWeight','bold','FontSize',14,'Color',[1 1 0]);
% Create title
title('Real Time Data','FontSize',15,'Color',[1 1 0]);
%% Initializing variables
voltage(1)=0;
time(1)=0;
count = 2;
k=1;
while ~isequal(count,loop)
%%Re creating Serial port before timeout
k=k+1;
if k==25
fclose(s);
delete(s);
clear s;
s = serial('com9');
fopen(s)
k=0;
end
&I GET ERRORS HERE:
invalues = Fscanf(s,'%f');
for k= 1 : length(invalues)
voltage(count) = invalues(k);
if voltage(count)-voltage(count-1) > MaxDeviation
voltage(count) = voltage(count-1)
end
time(count) = count;
count = count + 1;
end
set(plotHandle,'YData',voltage,'XData',time);
pause(TimeInterval);
end
Bruni
>
> &I GET ERRORS HERE:
> invalues = Fscanf(s,'%f');
humm,.. Why is it 'Fscanf' above and not 'fscanf' ?
--Nasser
The first input to fread, which you denote as s, should be a string. It
looks like it might be a struct, based on the use of s in the second input
to fread.
--
Loren
http://blogs.mathworks.com/loren/
http://www.mathworks.com/matlabcentral/
Hi Becky,
Considering that "s" is a serial object, the error indicates that you have no bytes available to read. Calling fread on a serial object with "zero" size as second argument will result in the error above.
You might want to have a check before you call fread to see if s.BytesAvailable is greater than zero.
Hope this helps
-Ankit
No no, that was just a typo here. its "fscanf" in my code. But i still get an error :(
Yes, the 's' represents my serial, which is why i deleted that line as i dont even need it. but i still get a warning "matching format failure" in my line "invalues=fscanf(s,'%f');" and i cant understand why!
_PLEASE_ fix your reader or learn to use the google interface to reply
within the same thread instead of creating a new one w/ every
posting????? :(
--
Then quit using google and go to The Mathworks site and use their portal
instead (altho surely it can't be _too_ hard to find a "REPLY"
button/link on the Google portal instead of
"Write/New/Whatever_it_is_you're_currently_using" to respond.
--
FREAD returns your an array of uint8 values (unsigned 8-bit integers). If you type "whos instring, you will probably see that it has class "uint8".
To use SSCANF, you will need to convert the uint8 arary to a character array, by using the CHAR function, e.g., char(instring).
Example:
>> data = uint8('3.14 456'); % sample data
>> mystring = char(data); % convert from uint8 to character array
>> whos data mystring % note the class of data and mystring
Name Size Bytes Class Attributes
data 1x8 8 uint8
mystring 1x8 16 char
>> sscanf(mystring,'%f')
ans =
3.1400
456.0000
Gautam
So in my case, where you have written "data = uint8('3.14 456'); % sample data", how can i change that so that matlab sees the data from my serial port continuously instead?