Varying variable name within one workspace

조회 수: 1 (최근 30일)
charlotte wilson
charlotte wilson 2017년 3월 13일
답변: Walter Roberson 2017년 3월 13일
folder = '1.5';
load comp.mat
figure
L1 = L;
J1 = J;
clearvars -except L1 J1
plot(L1,J1,'bo')
hold
folder = '1.8';
load comp.mat
L2=L;
J2=J;
clearvars -except L1 J1 L2 J2
plot(L2,J2,'b*')
From the code above I am trying to load a group of variable with all the same name and plot them all on one graph. I have been trying to rename the variable after they load but as soon as I load a new file it replaces the previous variable. Is there anyway I could rename the variables as they are imported and stop them from being replaced by the following variable?
Also if I could maybe save the variable with the Pt in the name EG: L_'PT' which varied dependent on input. This could solve the previous problem
load workspace.mat
prompt = 'What is the target frequency? '; %asks for values to input
Pt = input(prompt);
L = Pt/frequency;
BF = 1.88;
J = frequency/BF;
clearvars -except L J
save('comp');

채택된 답변

Walter Roberson
Walter Roberson 2017년 3월 13일
first = load('comp.mat', 'L', 'J');
plot(first.L, first.J, 'bo');
second = load('comp.mat', 'L', 'J');
plot(second.L, second.J, 'b*');
More generally...
dinfo = dir();
dinfo(~[dinfo.isdir]) = []; %zap non-directories
dinfo( ismember({dinfo.name}, {'.', '..'}) ) = []; %zap . and ..
nfold = length(dinfo);
vars = cell(nfold,1);
folders = {dinfo.name};
for K = 1 : nfold
thisdir = folders{K};
vars{K} = load( fullfile(thisdir, 'comp.mat'), 'L', 'J' );
plothandles(K) = plot(vars{K}.L, vars{K}.J, 'bo');
hold
end
legend(plothandles, folders)

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Object Identification에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by