How do I make a series of variables A1, A2, A3, ... A10?
Mostrar comentarios más antiguos
How do I make variables like this in a loop?
Respuesta aceptada
Más respuestas (2)
Pedro Inácio
el 14 de Oct. de 2017
Dear all, After read in different threads in Matlab forums, I found different solutions, which I believe they do not provide the real answer to: "Is possible to dynamically change variables names?"
The answer is Yes, and it does not need the use of eval.
I'm posting a solution to solve the question with comments step-by-step (It can be integrated in a for loop).
% NEW VARIABLE NAME
ID = 'new_NIF';
% FISCAL NUMBER SAVED IN MATLAB WORKSPACE
old_NIF = 555888222;
% CREATE A NEW PROVISORY STUCTURE
provisory_struct.(ID) = old_NIF;
% CREATE A NEW PROVISORY MAT FILE NAME
provisory_file_name = fullfile(pwd,'save_provisory_data.mat');
% SAVE THE PROVISORY STRUCTURE FIELDS AT THE
% PROVISORY MAT FILE
save(provisory_file_name,'-struct','provisory_struct');
% DELETE PROVISORY STRUCTURE VARIABLE
clear('provisory_struct');
% LOAD OUR NEW VARIABLE NAME
load(provisory_file_name,ID);
% DISPLAY NEW VARIABLE CONTENT
fprintf(1,'OLD NIF: %d\nNEW NIF: %d\n',old_NIF,new_NIF);
% DELETE PROVISORY FILE
delete(provisory_file_name);
I hope it was helpful.
3 comentarios
Image Analyst
el 14 de Oct. de 2017
I'm not sure how this is different than the solution Doug Hull gave
x2 = S.(sprintf('A%d', k));
and that the FAQ http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F gives at the end (same as Doug's code) -- they both use dynamic field names, just like you did when you did:
provisory_struct.(ID) = old_NIF;
The function eval is not the problem, no matter how much you think it is.
The problem is creating or accessing dynamically named variables, because doing so causes code to be slow, buggy, obfuscated, hard to debug, insecure, etc, etc.
What you have proposed is a well known solution, and it shares all of the same problems as eval, evalin, assignin, because of what it does: it dynamically creates variables. It does not matter what function you use to magically create variables with, doing so will always cause the same problems. The method that you have proposed is incredibly slow (much slower than eval), because it copies all data from memory onto the drive, and then from the drive back into memory. Totally inefficient, a total waste of time.
You should read Steve Lord's comment on load-ing straight into the workspace:
A discussion on the disadvantages of loading directly into the workspace:
And how it is better to load directly into an output variable:
Summary do not magically "poof" variables into existence: Always load into a structure, and never create variable names dynamically.
@Perdro: Saving the data to a file and relaoding them directly into the workspace has exactly the same drawbacks as eval'ing it directly, but in addition a massive delay due to the slow disk access. Of course, it "works", as eval does also. But it causes more troubles than it solves.
This topic has been discussed frequently and exhaustively in many forums. Your idea, that these discussions "do not provide the real answer", is off track.
M.Devaki Mohanarangam
el 14 de Jun. de 2023
Movida: Stephen23
el 14 de Jun. de 2023
0 votos
X = Slopevalue
LP1 = sum(X(:)<-5)/numel(X)*100;
LP2 = sum(X(:)<-4)/numel(X)*100;
LP3 = sum(X(:)<-3)/numel(X)*100;
LP4 = sum(X(:)<-2)/numel(X)*100;
LP5 = sum(X(:)<-1)/numel(X)*100;
LP6 = sum(X(:)<0)/numel(X)*100;
Lc1 = LP1;
Lc2 = LP2-Lc1;
Lc3 = LP3-(Lc1+Lc2);
Lc4 = LP4-(Lc1+Lc2+Lc3);
Lc5 = LP5-(Lc1+Lc2+Lc3+Lc4);
Lc6 = LP6-(Lc1+Lc2+Lc3+Lc4+Lc5);
how to apply loop condtion in matlab code
2 comentarios
Your verbose approach with anti-pattern numbered variable names:
X = -9*rand(1,7)
LP1 = sum(X(:)<-5)/numel(X)*100
LP2 = sum(X(:)<-4)/numel(X)*100
LP3 = sum(X(:)<-3)/numel(X)*100
LP4 = sum(X(:)<-2)/numel(X)*100
LP5 = sum(X(:)<-1)/numel(X)*100
LP6 = sum(X(:)<0)/numel(X)*100
Lc1 = LP1
Lc2 = LP2-(Lc1)
Lc3 = LP3-(Lc1+Lc2)
Lc4 = LP4-(Lc1+Lc2+Lc3)
Lc5 = LP5-(Lc1+Lc2+Lc3+Lc4)
Lc6 = LP6-(Lc1+Lc2+Lc3+Lc4+Lc5)
The simpler MATLAB approach using vectors:
LP = sum(X(:)<(-5:0),1)/numel(X)*100
LC = LP;
for k = 2:numel(LP)
LC(k) = LP(k)-sum(LC(1:k-1));
end
disp(LC)
The best approach:
LC = [LP(1),diff(LP)]
M.Devaki Mohanarangam
el 14 de Jun. de 2023
thank you stephan i will check this code
Categorías
Más información sobre Matrix Indexing en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!