Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Error: "First argument must be a string" in Matlab. PLZ HELP!!

2,701 views
Skip to first unread message

Becky91

unread,
Mar 26, 2011, 10:57:04 AM3/26/11
to
Hey guys,

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!

Nasser M. Abbasi

unread,
Mar 26, 2011, 11:24:09 AM3/26/11
to
On 3/26/2011 7:57 AM, Becky91 wrote:
> Hey guys,
>
> 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?
>

What does the command

class(instring)

show to you?


--Nasser

Becky91

unread,
Mar 26, 2011, 12:00:22 PM3/26/11
to n...@12000.org
What do you mean? That i shouldnt have a string in my class?

Nasser M. Abbasi

unread,
Mar 26, 2011, 12:04:41 PM3/26/11
to
On 3/26/2011 9:00 AM, Becky91 wrote:
> What do you mean? That i shouldnt have a string in my class?


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


ImageAnalyst

unread,
Mar 26, 2011, 12:36:18 PM3/26/11
to
On Mar 26, 12:00 pm, Becky91 <beckyza...@gmail.com> wrote:
> What do you mean? That i shouldnt have a string in my class?

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.

Becky91

unread,
Mar 26, 2011, 1:57:54 PM3/26/11
to n...@12000.org
You said:
> 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.
>


When i typed "class(instring)" it gave me
"ans = double"

What does that mean? Two arrays?

Becky91

unread,
Mar 26, 2011, 2:03:51 PM3/26/11
to ImageAnalyst
> 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.

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?

ImageAnalyst

unread,
Mar 26, 2011, 2:06:55 PM3/26/11
to
On Mar 26, 1:57 pm, Becky91 <beckyza...@gmail.com> wrote:
> When i typed "class(instring)" it gave me
> "ans = double"
>
> What does that mean? Two arrays?
--------------------------------------------------------------
Becky:
No. Will you please just look up the help for fread because it
explains everything? Now, how instring ended up as a double (64 bit
floating point number array) instead of a uint8 array like you asked
it to is unclear. I think you must have some code that you left out.

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.

Becky91

unread,
Mar 26, 2011, 2:14:47 PM3/26/11
to ImageAnalyst
You said:
> 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

Becky91

unread,
Mar 27, 2011, 6:18:13 AM3/27/11
to ImageAnalyst
Anyone? :S
Message has been deleted

Bruno Luong

unread,
Mar 27, 2011, 11:48:05 AM3/27/11
to
@Becky: please keep the question in by single thread. Thank you.

Bruni

Becky91

unread,
Mar 27, 2011, 5:01:03 PM3/27/11
to Bruno Luong
Just trying to get some help...!!

Nasser M. Abbasi

unread,
Mar 27, 2011, 5:19:21 PM3/27/11
to
On 3/26/2011 11:14 AM, Becky91 wrote:

>
> &I GET ERRORS HERE:
> invalues = Fscanf(s,'%f');

humm,.. Why is it 'Fscanf' above and not 'fscanf' ?

--Nasser

Loren Shure

unread,
Mar 28, 2011, 8:42:34 AM3/28/11
to

"Becky91" <becky...@gmail.com> wrote in message
news:5a832d5d-0a90-444a...@glegroupsg2000goo.googlegroups.com...

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/

Ankit Desai

unread,
Mar 28, 2011, 10:39:04 AM3/28/11
to
Becky91 <becky...@gmail.com> wrote in message <5a832d5d-0a90-444a...@glegroupsg2000goo.googlegroups.com>...

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

Becky91

unread,
Mar 28, 2011, 12:40:13 PM3/28/11
to n...@12000.org

No no, that was just a typo here. its "fscanf" in my code. But i still get an error :(

Becky91

unread,
Mar 28, 2011, 12:43:43 PM3/28/11
to Loren Shure

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!

Becky91

unread,
Mar 28, 2011, 12:45:58 PM3/28/11
to Ankit Desai
Yes, i checked s.BytesAvailable in my code and it told me i had 512! Which is why i got so confused as to why it didnt read anything. So i removed that line as i didnt need it in my code, but now im getting the warning: "matching format failure" in the line "invalues=fscanf(s,'%f');" and i have no idea why :S

Becky91

unread,
Mar 28, 2011, 2:18:37 PM3/28/11
to Ankit Desai
**Warning: Matching failure in format

Becky91

unread,
Mar 28, 2011, 2:19:09 PM3/28/11
to Loren Shure

dpb

unread,
Mar 28, 2011, 3:35:08 PM3/28/11
to
On 3/28/2011 11:45 AM, Becky91 wrote:
...

_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????? :(

--

Becky91

unread,
Mar 28, 2011, 4:58:10 PM3/28/11
to dpb
i dont know how to do that :S This is the first time im using google groups so be patient please.

dpb

unread,
Mar 28, 2011, 5:29:10 PM3/28/11
to
On 3/28/2011 3:58 PM, Becky91 wrote:
> i dont know how to do that ...

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.

--


Becky91

unread,
Mar 28, 2011, 5:56:37 PM3/28/11
to dpb
im using the "reply post"! whats so wrong with that? :S

Gautam Vallabha

unread,
Mar 29, 2011, 11:43:04 AM3/29/11
to
Becky91 <becky...@gmail.com> wrote in message <11cba566-5ff6-4af7...@glegroupsg2000goo.googlegroups.com>...

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

Becky91

unread,
Mar 29, 2011, 12:10:17 PM3/29/11
to Gautam Vallabha
> 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?

Becky91

unread,
Mar 29, 2011, 12:50:49 PM3/29/11
to Gautam Vallabha
Actually, your example really helped as i applied it to my needs. One last question though, my code works now but its displaying all my values in rows and columns. How can i display it on a graph instead please?
0 new messages