how to solve coding issue

조회 수: 12 (최근 30일)
Radwan
Radwan 2024년 7월 2일
답변: Sam Chak 2024년 7월 3일
Hello everyone
I have faced a problem in applying a code using my data
I don't know where is the problem in my code or data?
can anyone help
  댓글 수: 13
Sam Chak
Sam Chak 2024년 7월 3일
Your professor's code appears to be incomplete. If you insist on using it, we will not be able to assist with the missing parts, as they are user-defined variables (i.e., specific to your professor's implementation), not MATLAB functions.
In fact, there are several concepts in the code that you would need to learn, such as cell arrays like varnames and xlabel1, structure arrays like oo_.var_list and M_.exo_names, the use of for loops, and various MATLAB functions with miscellaneous name-value arguments and structure fields.
@Steven Lord and @Voss have explained some of these in their answer and comment. If the learning process is inflexible for you and you need to submit the code to your professor for evaluation, then you will need to familiarize yourself with all of these aspects before you can get the code working and identify the missing pieces.
Can you provide a list of what these different elements in the code do? This would demonstrate your effort and determination to understand the material, which could be helpful as you move forward.
figure()
findobj()
length()
set()
String
Children
LineWidth
Color
XGrid
YGrid
squeeze()
hold on, hold off
bar()
gca
XTick
XTickLabel
xlim()
legend()
title()
Radwan
Radwan 2024년 7월 3일
I got it @Sam Chak
Thank you very much
I actually did leave that code and I am using yours for graphs
I will look for code for another type of graphs needed
Thank you very much

댓글을 달려면 로그인하십시오.

채택된 답변

Steven Lord
Steven Lord 2024년 7월 2일
Just looking at your first for loop there are several problems or suggestions.
for i=14:22
figgcf=figure(i);
This line creates a new figure. Alternately if there is already a figure with that number open, it makes that figure the current figure. But since there is no line of code in the segment you posted that created any figures, this line ought to create a new figure.
axes_handles=findobj(figgcf,'type','axes');
This will return the handle of any[1] axes that exist in the figure you just created. But creating a figure doesn't automatically add an axes to it, so this will return an empty array. That means when you run the next line:
axsgca=axes_handles(2);
you're attempting to access the second element of an array that doesn't have a second element. [axes_handles doesn't even have a first element!] That's obviously not going to work, and that's why MATLAB threw the error you can see in your comment above, "Index exceeds the number of array elements. Index must not exceed 0."
set(axsgca,'XTick',datetick,'XTickLabel',datelabel,'XGrid','on');
If axsgca had been created, this line of code would have worked. Except it probably wouldn't have done what you wanted, since datetick has 12 elements and datelabel has only 5. So your axes would have the labels from datelabel repeated several times.
You may also be interested in the xticks and xticklabels functions.
axsgca.Title.String=varnames(i-13);
axsgca.XLabel.String='Date';
axsgca.YLabel.String='Percentage';
These ought to work, though I'd use the title, xlabel, and ylabel functions rather than directly setting the String property of those objects.
end
One broader problem is that you've created this figure and set properties on the axes (let's assume for sake of argument that you'd created an axes in the figure), but you haven't actually plotted anything. If you were to try to plot after this point, since you haven't turned hold on, MATLAB would reset the properties of the axes. So I'd recommend creating your plots first and then changing the ticks, grid, labels, and title after you've called plot or bar or other plotting functions.
Since you said you're new to MATLAB, I recommend that if you have access to some of the self-paced online courses that you work through them to familiarize yourself with MATLAB. In the Graphics category, the "Explore Data with MATLAB Plots" seems like a good first step, and "How MATLAB Graphics Work" would provide you with more information about more advanced Handle Graphics techniques.
[1] To those who say "But, actually, it's not all ..." I'm ignoring the axes HandleVisibility probability and findall for right now. That's an advanced maneuver.
  댓글 수: 2
Radwan
Radwan 2024년 7월 2일
Thank you very much for explaining all these things.
But I don't how to solve it.
My concern right now to fix this code to work on my data attached
could you please fix the current code
Steven Lord
Steven Lord 2024년 7월 2일
I could "fix" it by removing the attempt to set properties on an axes that doesn't exist. But somehow I don't think that's what you want.
Let's take a step back. In a comment on this answer, add a code block (using the button @Voss showed you.) Inside that code block, write comments (no code) describing the series of steps you want to implement. So if I were writing a code to deal out a poker hand, it would look something like:
% Create or get a deck of cards
% Shuffle the deck of cards
% Deal out five cards from the top of the deck
You don't have to get into all the details of your algorithm, but list the main parts of your workflow in enough detail that someone who isn't familiar with the workflow can at least understand an overview of it. [In the algorithm above I didn't specify what type of shuffle I used, for example.]
Knowing what you're trying to do may help us suggest how to achieve those steps, how to "fix" your code.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Sam Chak
Sam Chak 2024년 7월 3일
Hi @Radwan,
It's good to hear that you're making progress. If you encounter any issues with plotting other types of charts, feel free to come back to this thread and copy/paste your code here. Be sure to click on the 'Indentation' icon and the 'Play' icon to run the code and generate any error messages.
Actually, I find the previous plot is a bit cramped for display. You could consider creating a 2-by-2 grid of the bar graphs, as shown below:
T = readtable("usmodel_data.csv", VariableNamingRule="preserve");
%% extract data from table
x = T{:,1}; % data on x-axis
y = T{:,2}; % data on y-axis
%% make 2-by-2 tiles of bar graphs
tL = tiledlayout(2, 2, 'TileSpacing', 'Compact');
nexttile
xx = 2000:2003;
yy = reshape(y(1:16), 4, 4)'; % Group 4 quarters in one
bar(xx, yy), grid on, ylim([0 2.5e4])
nexttile
xx = 2004:2007;
yy = reshape(y(17:32), 4, 4)'; % Group 4 quarters in one
bar(xx, yy), grid on, ylim([0 2.5e4])
nexttile
xx = 2008:2011;
yy = reshape(y(33:48), 4, 4)'; % Group 4 quarters in one
bar(xx, yy), grid on, ylim([0 2.5e4])
nexttile
xx = 2012:2015;
yy = reshape(y(49:64), 4, 4)'; % Group 4 quarters in one
bar(xx, yy), grid on, ylim([0 2.5e4])
xlabel(tL, 'Year')
ylabel(tL, 'GDP')

카테고리

Help CenterFile Exchange에서 Title에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by