Understanding using the Plot() and troubleshooting with R2024a
Show older comments
So I am doing a simple project coupling with Simulink and an Arduino Uno. I built a model and used the arduino to generate data.
This is the code that I am using.
figure; plot(0:Ts:40,eo_act,0:Ts:40,ei,'r');
eo_act and ei are variables.
Ts is the assigned Sample time which is declared in the program in Simulink.
40 is the amount of time the program ran to collect the data.
So my problem is this: I keep on having this error code. But I don't know how to fix it or understand why it is popping up.
Error using plot
Data cannot have more than 2 dimensions.
Error in RCPulseM (line 76)
plot(0:Ts:40,eo_act,'r');
and
Error using plot
Data cannot have more than 2 dimensions.
Error in RCPulseM (line 76)
plot(0:Ts:40,eo_act,0:Ts:40,ei,'r');
I read the doc file assoiated with plots(), but it doesn't seem to explain for my situation. I have been experimenting with it. I separated each variable in their own respective plot()s and added a "hold on". It still throws the same error. I also verified that the variable has data in them.
Accepted Answer
More Answers (1)
Hi Scarlet,
The error message indicates that the data you are trying to plot has more than 2 dimensions. This usually means that one or more of your variables (eo_act or ei) are not vectors or 2D matrices, but rather 3D arrays or higher.
Here are a few steps you can take to troubleshoot and fix the issue:
- Use the size function to check the dimensions of eo_act and ei.
size(eo_act)
size(ei)
2. If eo_act or ei are 3D arrays or higher, you need to extract the relevant 2D slice or reshape them into a 2D matrix or vector.
3. Make sure that 0:Ts:40 has the same length as eo_act and ei.
4. Start with plotting one variable at a time to isolate the issue.
figure;
plot(0:Ts:40, eo_act);
hold on;
plot(0:Ts:40, ei, 'r');
hold off;
5. Sometimes, NaN or Inf values can cause issues with plotting.
any(isnan(eo_act(:)))
any(isnan(ei(:)))
any(isinf(eo_act(:)))
any(isinf(ei(:)))
Here's an example of how you might modify your code:
% Check dimensions
disp(size(eo_act));
disp(size(ei));
% Ensure eo_act and ei are vectors or 2D matrices
if ndims(eo_act) > 2 || ndims(ei) > 2
error('eo_act and ei must be vectors or 2D matrices.');
end
% Ensure consistency in dimensions
time_vector = 0:Ts:40;
if length(time_vector) ~= length(eo_act) || length(time_vector) ~= length(ei)
error('Time vector and data vectors must have the same length.');
end
% Plot the data
figure;
plot(time_vector, eo_act);
hold on;
plot(time_vector, ei, 'r');
hold off;
By following these steps, you should be able to pinpoint the issue and correct it.
Categories
Find more on Simulink 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!