how to solve coding issue
Show older comments
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 Comments
Steven Lord
on 2 Jul 2024
Without seeing a small sample of code and data and a clearer description of the problem you're facing, it's likely going to be difficult if not impossible to offer any help. Show us the code and/or data and ask a specific question about where you're experiencing difficulty and describe what's not working and we may be able to offer more guidance.
- Do you receive warning and/or error messages? If so the full and exact text of those messages (all the text displayed in orange and/or red in the Command Window) may be useful in determining what's going on and how to avoid the warning and/or error.
- Does your code do something different than what you expected? If so, what did it do and what did you expect it to do?
- Did MATLAB crash? If so please send the crash log file (with a description of what you were running or doing in MATLAB when the crash occured) to Technical Support so we can investigate.
Steven Lord
on 2 Jul 2024
When you write a comment, click the paper clip button in the Insert section of the toolstrip above the edit box where you write the comment. That will let you attach a data file (MAT-file, image file, spreadsheet, etc.) to your post.
For code, just copy and paste a small segment into the edit box. Before you do, click the first icon in the Code section of the toolstrip and paste the code into the area that adds in the edit box. Doing so will format the code the same way it's formatted in the MATLAB Editor.
And of course don't forget to ask a specific question once you've attached your data and included your code.
Radwan
on 2 Jul 2024
Voss
on 2 Jul 2024
@Radwan: You're welcome!
As Steven Lord said, "click the first icon in the Code section of the toolstrip and paste the code into the area that adds in the edit box." The icon is circled in green below:
I also ran your code (using the green triangular button in the Run section of the toolstrip). As you can see in your comment above, the code produces the error "Index exceeds the number of array elements. Index must not exceed 0." This is because the code expects figure 14 to have at least two axes. The problem is that, if figure 14 doesn't exist before the code is run, then the code creates it, but when a figure is created is has no axes.
That brings up the questions: What is the code intended to do? Does the code assume the existence of pre-existing figures with certain properties? How is the code related to the data file you shared? What is the variable "oo_" in the code? But the most important question is: What happens when you run the code that you would like help with?
Radwan
on 2 Jul 2024
Radwan
on 2 Jul 2024
Hi @Radwan
The CSV file is simply a table of data. It should be relatively straightforward to plot the selected columns of data.
If you are unfamiliar with your professor's code, I would advise against using it, as it may hinder your ability to learn and make you think that plotting simple data is more difficult in MATLAB than it needs to be. I suggest you plot the desired figure at your own pace. Then, you can learn how to make a bar graph by referring to some examples:
By working through the examples yourself, you will gain a better understanding of the bar() function and how to customize the plot to your needs.

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 a simple plot to see how it looks like
plot(x, y), grid on
xlabel('Year')
ylabel('GDP')
%% make a bar graph
xx = 2000:2023;
yy = reshape(y(1:96), 4, 24)'; % Group 4 quarters in one
bar(xx, yy), grid on
xlabel('Year')
ylabel('GDP')
Radwan
on 3 Jul 2024
Sam Chak
on 3 Jul 2024
Hi @Radwan
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
on 3 Jul 2024
Accepted Answer
More Answers (1)
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')
Categories
Find more on Environment and Settings 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!

