From: CuLa on
Hello, I have this simple program and anytime I run it from Matlab main window I get this error (Attemp to reference field of a non-structure array) right after the last line of the program. If I run it from the Editor by clicking the play button Run, there is no error. What's wrong?
-----------------------------
serial_port = 'COM8';
serial_speed = 115200;

s = serial(serial_port, 'BaudRate', serial_speed, 'Parity', 'none');

fopen(s);

opened = strcmpi(s.Status, 'open');
if (opened == 1)
fprintf('Port opened\n');
else
fprintf('Port did not open\n');
end
From: dpb on
CuLa wrote:
> Hello, I have this simple program and anytime I run it from
> Matlab main window I get this error (Attemp to reference field of
> a non-structure array) right after the last line of the program. If I
> run it from the Editor by clicking the play button Run, there is
> no error. What's wrong?
> -----------------------------
> serial_port = 'COM8';
> serial_speed = 115200;
>
> s = serial(serial_port, 'BaudRate', serial_speed, 'Parity', 'none');
>
> fopen(s);
>
> opened = strcmpi(s.Status, 'open');
> if (opened == 1)
> fprintf('Port opened\n');
> else
> fprintf('Port did not open\n');
> end


I see no corresponding fclose() so I presume probably your serial() call
fails to create the object when it already exists since the
configuration will be trying to create duplicate connections to the same
physical port.

You need to be sure to close and clear the remanants of a previous
attempt before trying again is my conjecture.

--
From: CuLa on
Thanks for your reply. I assume that this won't be the problem. I always manually close the port and clear all variables before starting this program again. Sorry for not mentioning it in the previous post. You see, after I run this, I read some data and work with it furthermore. Then I close it. If I didn't close it, then I would get an error, that the port is already used and it wouldn't work at all. Just to assure you, I added this fclose(s) command at the end of this program and the problem still persists.

The problem is that when I run the program in the Matlab Editor (the play button), I get no warning/attention. But when I run it directly from Matlab command window by typing >> program.m, I get the warning 'Attemp to reference field of a non-structure array' after the program ends. I really don't know, what problem there could be.
From: dpb on
CuLa wrote:
....
> The problem is that when I run the program in the Matlab Editor (the
> play button), I get no warning/attention. But when I run it directly
> from Matlab command window by typing >> program.m, I get the warning
> 'Attemp to reference field of a non-structure array' after the
> program ends. I really don't know, what problem there could be.

"program.m" isn't the syntax for calling a function named program, it's
the syntax to address the field "m" of a structure named "program".

The syntax is simply

"program"

if the file is program.m and the function is called program.

I'd suggest a more meaningful name choice, but that's immaterial to the
problem.

--

From: Walter Roberson on
dpb wrote:
> CuLa wrote:
> ....
>> The problem is that when I run the program in the Matlab Editor (the
>> play button), I get no warning/attention. But when I run it directly
>> from Matlab command window by typing >> program.m, I get the warning
>> 'Attemp to reference field of a non-structure array' after the
>> program ends. I really don't know, what problem there could be.
>
> "program.m" isn't the syntax for calling a function named program, it's
> the syntax to address the field "m" of a structure named "program".
>
> The syntax is simply
>
> "program"
>
> if the file is program.m and the function is called program.
>
> I'd suggest a more meaningful name choice, but that's immaterial to the
> problem.


Amplifying slightly:

program.m

could cause the function named "program" to run with no arguments, and
then attempt to access the field "m" of the result.