Finding a double in the workspace by name and assigning it to a new variable

조회 수: 8 (최근 30일)
Hi there,
I've got a very simple question. I've got some very large files which I'm iteratively uploading. I assign part of each file's name to a variable "ID". Say, in my example, I've already successfully done that, so I have:
ID = 100 % participant number 100
In addition, I've got many different double type variables in my workspace, of different length. They are named: "p100", "p101", "p102", "p103", etc. For example, the "p100" contains:
p100=[1;0;1;1;0;1;1;1;1;1;1;0;1;1;1;1;0;0;1;1;1;1;1;1;0;1;1;1;1;1;1;0;1;1;1;1;1;1;1;1;1;1;1;0;1;1;1;1;1;0;1;1;1;1;1;0;1;1;0;1;1;1;1;1;1;1;1;1;1;0;0;1;0;0;1;0;0;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;0;1;1;1;1;1;1;1;1;1;0;1;1;0;1;1;0;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;0;1;1;1;0;1;1;1;1;0;1;1;1;1;1;0;1;1;1;1;1;1;1;1;1;1;1;1;1;1;0;1;1;1;1;1;1;1;1;0;1;1;1;1;1;1;1;1;1;0;1;0;1;1;1;1;1;1;0;0;1;1;1;0;1;1;1;1;0;1;1;1;1;1;1;1;0;1;1;1;0;1;1;1;1;1;1;1;0;0;1;1;1;1;1;1;0;0;1;0;1;1;0;1;1;1;1;1;1;1;1;1;0;0;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;1;0;1;0;1;1;1;1;0;1;1;1;1;0;1;1;1]
What I want to do is match the ID of my participant 100 to the correct double variable (p100) by name, so that I can copy that double into a new variable ("desired_variable"). Here's what I have:
desired_variable_name = who(strcat('p',num2str(ID)))
It correctly outputs "p100", which is my desired double variable name. I cannot, however, understand how to call this variable from the workspace by it's name, so that I can copy its contents of zeros and ones (and not just the name) into the new variable "desired_variable" that I can work with later on. I tried different things with "find", "disp", the apparently nonexistent in this language "print", etc. Any suggestions?
  댓글 수: 1
Stephen23
Stephen23 2023년 3월 28일
편집: Stephen23 2023년 3월 28일
"I've got a very simple question."
Very simple answer: don't do this.
"Any suggestions?"
Really don't do this:
Better data design will make this task easy (and your code simpler and more efficient).
"In addition, I've got many different double type variables in my workspace, of different length. They are named: "p100", "p101", "p102", "p103", etc. "
You did not tell us the most important information: how did you get all of those variables into the workspace? Hopefully you did not write all of their names out by hand, which means you probably created them using some code or by LOADing some (badly-made) MAT files.... which is exactly the place where you should fix your code.
For example, rather than LOADing directly into the workspace, you should always LOAD into an output variable (which is a scalar structure):
S = load(..)
"I cannot, however, understand how to call this variable from the workspace by it's name..."
If you upload some of your data files, we can show you how to import them properly, so that you can avoid this ... situation.

댓글을 달려면 로그인하십시오.

채택된 답변

Luca Ferro
Luca Ferro 2023년 3월 28일
편집: Luca Ferro 2023년 3월 28일
this is for a single entry:
ID=100;
evalin('base',strcat('desired_variable_name=p',string(ID)))
i don't know what ID looks like but you can loop through it easily:
ID=100:110; %my guess of what ID looks like.
for ii=1:numel(ID)
evalin('base',strcat('desired_variable_name_',string(ii),'=p',string(ii)));
end
is it a good idea to use evalin to generate variables and give values to them? no
note: if your goal is not to assign them in the base workspace but in the function one (you never specified if it is a script or a function), replace 'base' with 'caller'.

추가 답변 (1개)

Image Analyst
Image Analyst 2023년 3월 28일
"They are named: "p100", "p101", "p102", "p103", etc. "
Well that's your biggest mistake. Don't do that. Use an array. Why not?

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by