Whats wrong with this matlab code?

Click For Summary

Discussion Overview

The discussion revolves around troubleshooting a MATLAB code that is intended to generate a graph but fails to do so, resulting in an unexpected output on the command line. Participants explore potential issues related to the code's execution and provide suggestions for resolution.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant reports that the code runs fine in MATLAB R2006b and suggests removing the semicolon at the end of the for loop.
  • Another participant indicates that they can run the code in MATLAB R11.1 and see a figure, but nothing appears in the command line.
  • A participant proposes that the code might be plotting each point in a separate graph and suggests storing results in an array to plot after the loop.
  • One participant mentions that the "hold on" command allows plotting different values for q_0 on the same graph.
  • A participant notes that the error was due to the M-file name containing a white space.

Areas of Agreement / Disagreement

There are multiple competing views regarding the cause of the issue and how to resolve it, with no consensus reached on a definitive solution.

Contextual Notes

Participants mention different versions of MATLAB, which may affect the behavior of the code. There is also uncertainty about how the code is being executed (e.g., from the command line vs. the m-file editor).

Who May Find This Useful

Individuals troubleshooting MATLAB code, particularly those working with plotting functions and version-specific issues.

ranger
Gold Member
Messages
1,685
Reaction score
2
I have a very simply MATLAB code. When I try go run it, I get no graph and switching over to the MATLAB command line, I see:
ans =

char

Here is the code:
Code:
t = linspace(0,60);
v = 3.22;

for q_0 = [0:1:2007];
    q = q_0*exp(v*t);
    plot(t,q)
    hold on
end
It looks just fine to me.

Any feedback would be great.
 
Physics news on Phys.org
It runs fine in MATLAB R2006b on my computer.

Perhaps try removing the semicolon at the end of "for q_0 = [0:1:2007];"

- Warren
 
I still get no luck. I can run other m-files from the same directory with no problems. Does it say anything on the MATLAB command line when you run it?
 
I have copied-and-pasted the script on the Matlab (R11.1) command prompt and a figure appears. Nothing appears in the command line though.

How did you try to run the script?
 
It looks to me to be plotting every point in a separate graph,, try writing to an array and plot after the loop is complete. I;ll try at work tommorow where I have Matlab R2006B.
 
doodle said:
I have copied-and-pasted the script on the Matlab (R11.1) command prompt and a figure appears. Nothing appears in the command line though.

How did you try to run the script?

I tried to run it from the m-file editor screen. The usual Debug>Run. I also even tried to get it from the command line, but I still get ans = char as an output.

Dr Transport said:
It looks to me to be plotting every point in a separate graph,, try writing to an array and plot after the loop is complete. I;ll try at work tommorow where I have Matlab R2006B.
I don't think its separate graphs. Its done on one graph but the "hold on" command allows me to plot different values for q_0 on the same graph.


Its is possible for anyone to post a screenshot of the graph? btw, I'm using R14.
 
ranger said:
Its is possible for anyone to post a screenshot of the graph? btw, I'm using R14.

Here's a screenshot...
 

Attachments

  • 01.jpg
    01.jpg
    34 KB · Views: 539
Thanks doodle. It seems I have to do a little tweaking of my domain, but its what I expected it to be.
 
It turns out I was getting this error because the name of the M-file had a white space. Beats me...
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
4K
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 11 ·
Replies
11
Views
4K
  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 9 ·
Replies
9
Views
3K