Looping through tables in my workspace, plotting them, and saving a picture of the plot.
Show older comments
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 Comment
Stephen23
on 17 Oct 2023
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.
Accepted Answer
More Answers (1)
Matthew Blomquist
on 16 Oct 2023
1 vote
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 Comments
Logan
on 17 Oct 2023
When I ran the code with only tables in the workspace, it appeared to work correctly, and I got the .png files.
Here's a demo:
load('Workspace example.mat')
clear ans cmdout % these variables exist in the online environment workspace.
% clear them to have only whatever is in 'Workspace example.mat' remaining
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
% list the .png files in the current directory:
ls *.png
% display one of the .png files:
imshow([tableNames{1} '_plot.png'])
Logan
on 17 Oct 2023
Matthew Blomquist
on 17 Oct 2023
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?
Logan
on 17 Oct 2023
Categories
Find more on Convert Image Type 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!
