For loop in Matlab that takes data from workspace

11 次查看(过去 30 天)
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.

采纳的回答

Adam Danz
Adam Danz 2018-11-28
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.
  • 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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by