Understanding using the Plot() and troubleshooting with R2024a

3 次查看(过去 30 天)
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.

采纳的回答

Ayush
Ayush 2024-7-31
编辑:Ayush 2024-7-31
From the error message that you have shared, it seems the problem is regarding the vector that you are feeding into the "plot" function as it only accepts 1-D or 2-D arrays. You would first need to ensure the dimensions of the variables, eo_act and ei. Additionally, you should also verfiy the sample time length to match the length of the aforementioned variables. Once the verification is set in place and if necessary, refactoring of the dimensions is done, you can again utilize the "plot" function. For more information on the "plot" function, you can refer to the following documentation:
Here is an example code for your reference:
% Check dimensions for the variables
disp(size(eo_act));
disp(size(ei));
% Ensure both variables are 2-D vectors
eo_act = eo_act(:);
ei = ei(:);
% Verify time vector length matches data length
time_vector = 0:Ts:40;
assert(length(time_vector) == length(eo_act), 'Length of time vector and eo_act must match');
assert(length(time_vector) == length(ei), 'Length of time vector and ei must match');
This approach would give you much added clarity where the dimensions are not compatible.

更多回答(1 个)

Shubham
Shubham 2024-7-31
编辑:Shubham 2024-7-31
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:
  1. 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.

类别

Help CenterFile Exchange 中查找有关 Polar Plots 的更多信息

标签

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by