Looping through tables in my workspace, plotting them, and saving a picture of the plot.
조회 수: 18 (최근 30일)
이전 댓글 표시
Hello, in my workspace I have tables, they all contain a column for time and a column for signal. In the code I have attached I am trying to loop through the tables in the workspace, plot them onto a figure and save the resultant figure. However, the code keeps getting stuck at:
FP.rawdata(:,1) = currentTable.time;
The error message I receive is:
Dot indexing is not supported for variables of this type. I think where
currentTable = evalin('base', tableName);
the evalin is not currently assigning the table I am looking at into the temporary currentTable variable. Which is weird because when I try running the lines one by one in the command window, currentTable is correctly displaying the contents of tableName(1).
If someone could help trouble shoot this code help me figure this out I will greatly appreciate it:
I have attached the code
tableNames = who;
for i = 1:length(tableNames)
tableName = tableNames{i};
currentTable = evalin('base', tableName);
FP.rawdata(:,1) = currentTable.time;
FP.rawdata(:,2) = currentTable.signal;
startpoint = FP.rawdata(1,1);
FP.rawdata(:,1) = (FP.rawdata(:,1) - startpoint);
FP.calcium_dependent(:,1) = downsample(FP.rawdata(2:end,1),2);
FP.calcium_dependent(:,2) = downsample(FP.rawdata(2:end,2),2);
FP.isosbestic(:,1) = downsample(FP.rawdata(:,1),2);
FP.isosbestic(:,2) = downsample(FP.rawdata(:,2),2);
temp_fit1 = fit(FP.calcium_dependent(:,1),FP.calcium_dependent(:,2),'exp2');
temp_fit2 = fit(FP.isosbestic(:,1),FP.isosbestic(:,2),'exp2');
%Generate and save figure as a picture
figure(1)
plot(FP.calcium_dependent(:,1),FP.calcium_dependent(:,2)-temp_fit1(FP.calcium_dependent(:,1)),'b');
hold on
plot(FP.isosbestic(:,1),FP.isosbestic(:,2)-temp_fit2(FP.isosbestic(:,1)),'r');
grid on
ylabel({'Linearized Raw fluorescence'});
xlabel({'Time (seconds)'});
title({tableName ', ACh SNFR in dHPC'});
legend ACh-dependent-signal Isosbestic
saveas(gcf, [tableName '_plot.png']);
close all
clear FP
end
댓글 수: 1
Stephen23
2023년 10월 17일
Having lots of variables in the workspace and then trying to access them dynamically is the immediate cause. That in turn is the result of the bad design decision to force meta-data into the variable names. Best avoided:
You can avoid the whole thing by e.g. using arrays and indexing.
Note that meta-data (e.g. filenames, test parameters, etc) is data, and data is best stored in variables (not in variable names)... and then you can start to write simpler, more robust code that does not rely on evil EVALIN or ASSIGNIN.
채택된 답변
Voss
2023년 10월 16일
The error message, "Dot indexing is not supported for variables of this type." indicates that some code is trying to access a column of a table (or a field of a struct), but the variable is not a table (not a struct, respectively). I suspect that happens on the line you point out:
FP.rawdata(:,1) = currentTable.time;
and it happens because currentTable is not a table.
This would happen if your workspace contains non-table variables in addition to the tables you care about. For example, perhaps you start with a workspace containing only tables but then you run your script, which populates the workspace with some other non-table variables as well that don't get cleared, e.g., temp_fit_1, temp_fit_2; then when you run the script again, those non-table variables still exist, their names are stored in tableNames, your code tries to treat them as tables even though they are not, and you get the error.
One way to fix this would be to make sure tableNames contains only names of tables (i.e., no names of any variables of any other class) in the workspace, and one way to do that is to clear the non-tables before calling tableNames = who. Something like this:
% clear the non-table variables:
S = whos();
S(strcmp({S.class},'table')) = [];
vars_to_clear = {S.name};
if ~isempty(vars_to_clear)
clear(vars_to_clear{:});
end
clear S vars_to_clear
% get the table names:
tableNames = who;
% etc. (rest of code the same)
댓글 수: 8
Voss
2023년 10월 17일
The error about unrecognized variable 'data' was due to not reading the file (readtable was only being called when both 0G and 1G were in the file name).
I'm any case, you're welcome and I'm glad it's working now!
추가 답변 (1개)
Matthew Blomquist
2023년 10월 16일
I downloaded your mat file and tested your code and it works fine for me. I'm guessing the error occurs because of the first line: "tableNames = who"
Is your workspace clear of all variables except the n x 2 tables? When I ran your code a second time (after successfully running it the first time), I received the same error as above because not all the variables in my workspace were the n x 2 tables.
Let me know if that works, and if it doesn't, I can look closer!
댓글 수: 5
Matthew Blomquist
2023년 10월 17일
What is the output of
tableNames = who
(without the semi colon to suppress the output) as Voss had in his code above? Is it just the tables that you want, or are there extra variables that are not tables included?
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!