Error when using (plotting) one m file data into another m file.
Show older comments
Hello all,
Here I'm facing a problem, I have four files: file A.m, file B.m, file C.m, and file D.m. And one more file to plot data which is plot.m, now at very start I putted all other files(A,B,C,and D) in the plot.m. However, some varibles are not appearing in the figure and error "undefine varible" appear when plotting. While most of the varibles are in file C and D, the file C appears okay but file D doesnot, and mysteriously when I change name position of file C and file D then both shows error that is (C.m D.m plots appers for C.m only and problem appears in D.m) then if change position and write like this way (D.m and C.m) then error occurs. Point to note here C.m and D.m also use A.m and B.m in the startd. Please if you know the solution.
7 Comments
Sindar
on 23 Mar 2020
What do the scripts A-D do? Are there any "clear" commands in them?
Adam Danz
on 23 Mar 2020
Are the m files all scripts (see scripts vs functions)? My guess is that one script depends on variables defined in another script and then the scripts are run out of order, you've got missing variables.
This is one reason my scripts are messy to work with.
The orange/red underlines and the orange right-side-tickmarks visible in the editor should show you warning/error messages and those messages should point to the problem.
shane watson
on 25 Mar 2020
Edited: shane watson
on 25 Mar 2020
Adam Danz
on 25 Mar 2020
There could be several problems and if the same variable name is used in multiple scripts that could definitely cause a problem. This is why it's recommended to avoid using scripts unless all of the code is contained within a single script (even then, a function is better, IMO).
" I didn't clearly get your last point."
When you view the code from the editor, you should see orange squiggly lines under sections that contain warning messages. You'll also see an organe tick mark in the right margine and the square at the top of the right side is orange. If the problem would cause an error, the colors would be red instead of orange.
Notice the variable a is assigned a value twice, below.

When you hover over the orange squiggle line,

When you hover over the orange tick on the right,

shane watson
on 28 Mar 2020
shane watson
on 1 Apr 2020
Answers (1)
Adam Danz
on 1 Apr 2020
0 votes
(Comment copied to answers section).
There could be several problems and if the same variable name is used in multiple scripts that could definitely cause a problem. This is why it's recommended to avoid using scripts unless all of the code is contained within a single script (even then, a function is better, IMO).
" I didn't clearly get your last point."
When you view the code from the editor, you should see orange squiggly lines under sections that contain warning messages. You'll also see an organe tick mark in the right margine and the square at the top of the right side is orange. If the problem would cause an error, the colors would be red instead of orange.
Notice the variable a is assigned a value twice, below.

When you hover over the orange squiggle line,

When you hover over the orange tick on the right,

Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!