MATLAB Answers

Got an Error Using Grid -Unknown Command Option

1 view (last 30 days)
SITI HASANAH
SITI HASANAH on 26 Jun 2020
Edited: John D'Errico on 26 Jun 2020
Hello, I write my coding on editor and want to plot the graph but i got some error where it said that "Error Using Grid-Unknown Command option".but stil it appear only graph figure 1 and figure 2 tu did not appear ..What I confuse the error said line on figure 1 but why still i get the graph but not figure 2? The Graph of figure 2 did not appear and did not get error?
Below is my coding:
fid = fopen('abc.txt','wt');
vol=zeros(6,4);
Percentage=zeros(6,4);
Vact= [31.06300,40.29600,49.52700,58.75800
6.209400,8.057700,9.904700,11.75130
0.306610,0.401110,0.494380,0.587210
0.073430,0.098860,0.122920,0.146530
0.039958,0.055665,0.069856,0.083571
0.026436,0.038378,0.048629,0.058391];
P = [0.01,0.05,1,4,7,10];
T = [673.15,873.15,1073.15,1273.15];
for i=1:6
for j=1:4
vol(i,j) = ((0.000461631).*T(j))./P(i);
end
end
% Calculate %error
for i=1:6
for j=1:4
Percentage(i,j) = 100.0*(vol(i,j)-Vact(i,j))/Vact(i,j);
end
end
% Table header estimated z
fprintf(fid,'Estimated z');
fprintf(fid,'\n 2 4 6 \n');
fprintf(fid,' -------------------------------------------------------------\n');
% Print estimated z table
for i=1:6
fprintf(fid,' | %6.2f | %6.5f | %6.5f | %6.5f | %6.5f |\n',P(i), vol(i,1), vol(i,2), vol(i,3), vol(i,4));
end
% Table header % error
fprintf(fid,'\nEstimated errors');
fprintf(fid,'\n 2 4 6\n');
fprintf(fid,' ---------------------------------\n');
% Percentage error table
for i=1:6
fprintf(fid,' | %6.2f | %6.2f %6.2f %6.2f %6.2f | \n',P(i), Percentage(i,1), Percentage(i,2), Percentage(i,3), Percentage(i,4));
end
figure (1)
plot(T,vol,'k-'),xlabel('Temperature(K)'),ylabel('Estimated Specific Volume,m^3/kg'),grid title('Graph of Estimated Volume versus Temperature');
figure (2)
plot(T,Percentage,'k-'),xlabel('Temperature(K)'),ylabel('Percentage error(%)'),grid title('Graph of Percentage error versus Temperature');
fclose(fid);

  0 Comments

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 26 Jun 2020
grid title('Graph of Estimated Volume versus Temperature')
grid() does not have a 'title' option.
Perhaps you wanted
grid on, title('Graph of Estimated Volume versus Temperature')

  0 Comments

Sign in to comment.


John D'Errico
John D'Errico on 26 Jun 2020
Edited: John D'Errico on 26 Jun 2020
You don't get charged less for MATLAB, if you save on lines of code. They also don't charge less if you use another comma.
You wrote this as one line:
plot(T,vol,'k-'),xlabel('Temperature(K)'),ylabel('Estimated Specific Volume,m^3/kg'),grid title('Graph of Estimated Volume versus Temperature');
And, effectively, you want MATLAB to know that it is intended as a series of lines, to be evaluated one after another, like this:
plot(T,vol,'k-')
xlabel('Temperature(K)')
ylabel('Estimated Specific Volume,m^3/kg')
grid title('Graph of Estimated Volume versus Temperature');
Oh. Wait. What is that last line? What I've shown you is how MATLAB will interpret the very last line you wrote.
Did you put a comma after the call to grid, between grid and title? No? So what happens is it tries to resolve that line. It gets confused. Is this a call to the function grid? But what is all that other stuff there? So it tries to pass that string into the function grid, as an argument, using grid as a command form. But that gets it more confused.
(By the way, this gets into the command/function duality thing in MATLAB, where all commands are actually viewed as function calls.)
Not surprsingly, this runs fine:
plot(T,vol,'k-')
xlabel('Temperature(K)')
ylabel('Estimated Specific Volume,m^3/kg')
grid
title('Graph of Estimated Volume versus Temperature');
Although the run on mess, when repaired with a spare comma will also work.
plot(T,vol,'k-'),xlabel('Temperature(K)'),ylabel('Estimated Specific Volume,m^3/kg'),grid, title('Graph of Estimated Volume versus Temperature');
I would point out that making your code more readable has other benefits. It will be come easier to read and debug. And easy to debug code is a REALLY valuable thing to have.
You will also need to repair the second plot call too, in the same way.
As for not getting a second graph, or even an attempt to plot figure 2, when it encounters an error, MATLAB STOPS running the code you give it. So at the point where the error was generated, it had given up. This is not the case for a warning message. Errors are final.

  0 Comments

Sign in to comment.


Translated by