For loop in Matlab that takes data from workspace
11 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Raniely Candelario
am 28 Nov. 2018
Kommentiert: Stephen23
am 28 Nov. 2018
I have raw data from a series of tensile tests. The data from the various samples were imported from excel and are in table format. They are named C100_1, C200_1, etc. I have written a script that will extract data from one table and plot a stress-strain curve as well as calculate mechanical properties.
My question is as follows: Is it possible to have a for loop that will go through all the table variables in my workspace in one go. Currently my solution is just to change the code each time and input the name of the variable and run manually.
Thanks in advance.
1 Kommentar
Stephen23
am 28 Nov. 2018
You should read this:
and then import your tables into one array (e.g. a cell array).
Akzeptierte Antwort
Adam Danz
am 28 Nov. 2018
Is it possible...?" Yes. But it's not a good idea.
Good idea
Presumably you have code that loads the tables into your workspace. Ideally you would have a variable that stores all of the tables and this variable could be created upon loading the data. In this example, your tables are stored in a cell array. Then you pass that cell array into code that loops through each table.
myTables = {C100_1, C200_1}; %store all tables in cell array
function fakeFunction(myTables) %pass the cell array into a function
for i = 1:length(myTables) %loop through each table
doStuff(myTables{i});
end
end
Horrible idea
I won't give too much detail here because this idea is bad. The who() function lists all variables in a workspace. The function evalin() can be used to get variables from different workspace (though shouldn't be used for this). The class() function determines the class of each variable and can be used to identify if a variable is a table or not. So, it's possible to list all variables (in any workspace) and determine if they are tables or not within a loop, but this opens up a can of worms and the following problems:
- Your code won't know the difference between the tables you actually want to analyze and any other tables that might appear in the workspace.
- You can't control at all which tables you expect to be processed. If you are missing a table, you'll never know it was missing.
- Using dynamic variables is risky, ugly, and looked down upon.
- If your colleagues see that you've done this, they will talk about you in the lunch room.
In summary, you want to find a way to group your tables together (in a cell array or a structure) and send that single group into a function that loops through each element.
1 Kommentar
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!