Finding a double in the workspace by name and assigning it to a new variable
Show older comments
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 Comment
"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.
Accepted Answer
More Answers (1)
Image Analyst
on 28 Mar 2023
0 votes
"They are named: "p100", "p101", "p102", "p103", etc. "
Well that's your biggest mistake. Don't do that. Use an array. Why not?
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!