want to change what data I am plotting by text name.

2 次查看(过去 30 天)
I am needing to plot many things from a .mat file but do not want to copy and paste the same code 30 times. I am wanting to set a varible such as 'CylinderPressure' and call it later. This works for everything but plotting. How can I change the 'CylinderPressure' so whats in it (the varible in question) can be referenced in plotting. In the end I want to just change the var variable to change what I am plotting. I atached the code I want to modify. Any help would be greatly apreciated.
var = 'CylinderPressure';
hold on
load('march.mat', var)
plot(CylinderPressure)
load('Feb.mat', var)
plot(CylinderPressure)
title(var)
legend('march','feb')
hold off

采纳的回答

Stephen23
Stephen23 2023-6-13
编辑:Stephen23 2023-6-13
Do NOT load directly into the workspace, always LOAD into an output variable (which is a scalar structure).
Then simply use this syntax:
var = 'CylinderPressure';
S1 = load('march.mat',var);
S2 = load('Feb.mat' ,var);
V1 = S1.(var);
V2 = S2.(var);
hold on
plot(V1)
plot(V2)
title(var)
legend('march','feb')
hold off

更多回答(0 个)

类别

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

标签

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by