why are the names not being saved as written? Why do i get this error each time? Also why is the legend wrong? please fix this so i can verify my code is working as intended.

1 次查看(过去 30 天)
Hi so the second code calculates the SEM of each struct (std error of mean) and adds it to the mat file and gives you an updated mat file. But I can't change the name to what I want so I just leave it as features. for ex: features1SEM without this error:
Error using save
Variable 'features1SEM' not found.
Error in semcalculationsbetweenfields (line 20)
save('featuresLD2.mat', 'features1SEM');
The first code plots the data and the sem with the space between them shaded in to give area style fill. but i noticed that despite what i name them they are loaded differntly hence why the load file name is different than the name in the for loop. can someone explain why and confirm that the sem calculated is the sem of the structs and that its plotted correctly?Also In the legend i try to make the first 3 data sets a different color from the next 3 but the lines in the legend don't necessarily allow this or at least I am not sure how to do it. Can you please help me to also make the legend accurately reflect the data plotted? Thank you!
Plotting SEM
clear all
% Load the mat file
load('features1SEMa.mat');
load('features2SEMa.mat');
% Plot the total area for each feature in each mat file
subplot(3,3,1)
hold on;
% Plot Lobule 1-3 in blue
for i = 1:3
x = 13 - (1:length(features1SEM(i).totalArea));
y = features1SEM(i).totalArea;
SEM = features1SEM(i).totalArea_SEM;
y_upper = y + SEM;
y_lower = y - SEM;
fill([x, fliplr(x)], [y_upper, fliplr(y_lower)], 'blue', 'FaceAlpha', 0.2, 'EdgeColor', 'none');
plot(x, y, 'LineWidth', 2, 'color', 'blue');
end
% Plot Lobule 4-6 in red
for i = 1:3
x = 13 - (1:length(features(i).totalArea));
y = features(i).totalArea;
SEM = features(i).totalArea_SEM;
y_upper = y + SEM;
y_lower = y - SEM;
fill([x, fliplr(x)], [y_upper, fliplr(y_lower)], 'red', 'FaceAlpha', 0.2, 'EdgeColor', 'none');
plot(x, y, 'LineWidth', 2, 'color', 'red');
end
xlim([1, length(features1SEM(i).totalArea)]);
xticks(1:length(features1SEM(i).totalArea));
xlabel('PP - PC axis');
ylabel('Lipid Droplet Count');
legend('(\color{blue}Lobule 1-3\color{black})', '(\color{red}Lobule 4-6\color{black})');
title('Lipid Droplet Count');
Calculating SEM
clear all
% Load the mat file
load('featuresLD2.mat');
% Calculate SEM between fields for each feature
fieldnames = {'totalArea', 'AvgCircularityy', 'profileCounts', 'zoneArea', 'AvgFeret', 'avgSize'};
for j = 1:numel(fieldnames)
data = [features(1).(fieldnames{j}), features(2).(fieldnames{j}), features(3).(fieldnames{j})];
SEM = std(data) / sqrt(numel(data));
% Add SEM to the feature structure for each feature
for i = 1:3
fieldname_with_SEM = [fieldnames{j} '_SEM'];
features(i).(fieldname_with_SEM) = SEM;
end
end
% Save the updated mat file
save('featuresLD2.mat', 'features');

回答(1 个)

Steven Lord
Steven Lord 2023-5-19
Which line of code is actually part of your program? The line from the error message:
save('featuresLD2.mat', 'features1SEM');
does not appear in any of the code you posted. The closest line that does is the last line of your second file:
save('featuresLD2.mat', 'features');
You start off that second file with clear all so only the variables you load from featuresLD2.mat in that file or create inside that file exist in the workspace. Nowhere do you create or modify a variable named features1SEM, so the error message makes sense if that line from the error message is what's actually in your code (and that variable is not part of featuresLD2.mat.)
  2 个评论
Laura
Laura 2023-5-20
@Steven Lord using the two mat files that I provided you can use my code on each one.
clear all
% Load the mat file
load('featuresLD2.mat');
% Calculate SEM between fields for each feature
fieldnames = {'totalArea', 'AvgCircularityy', 'profileCounts', 'zoneArea', 'AvgFeret', 'avgSize'};
for j = 1:numel(fieldnames)
data = [features(1).(fieldnames{j}), features(2).(fieldnames{j}), features(3).(fieldnames{j})];
SEM = std(data) / sqrt(numel(data));
% Add SEM to the feature structure for each feature
for i = 1:3
fieldname_with_SEM = [fieldnames{j} '_SEM'];
features(i).(fieldname_with_SEM) = SEM;
end
end
% Save the updated mat file
save('featuresLD2.mat', 'features');
However, when I try to plot it with my following code i get two issues 1. the legend doesn't match the graph (i.e. first 3 structs are red the next 3 structs are blue but the lines in the legend are not) how can i fix this? 2. Can you confirm that the sem calculated is the sem of all 3 the structs in each mat file and that its plotted correctly in my code?
clear all
% Load the mat file
load('features1SEMa.mat');
load('features2SEMa.mat');
% Plot the total area for each feature in each mat file
subplot(3,3,1)
hold on;
% Plot Lobule 1-3 in blue
for i = 1:3
x = 13 - (1:length(features1SEM(i).totalArea));
y = features1SEM(i).totalArea;
SEM = features1SEM(i).totalArea_SEM;
y_upper = y + SEM;
y_lower = y - SEM;
fill([x, fliplr(x)], [y_upper, fliplr(y_lower)], 'blue', 'FaceAlpha', 0.2, 'EdgeColor', 'none');
plot(x, y, 'LineWidth', 2, 'color', 'blue');
end
% Plot Lobule 4-6 in red
for i = 1:3
x = 13 - (1:length(features(i).totalArea));
y = features(i).totalArea;
SEM = features(i).totalArea_SEM;
y_upper = y + SEM;
y_lower = y - SEM;
fill([x, fliplr(x)], [y_upper, fliplr(y_lower)], 'red', 'FaceAlpha', 0.2, 'EdgeColor', 'none');
plot(x, y, 'LineWidth', 2, 'color', 'red');
end
xlim([1, length(features1SEM(i).totalArea)]);
xticks(1:length(features1SEM(i).totalArea));
xlabel('PP - PC axis');
ylabel('Lipid Droplet Count');
legend('(\color{blue}Lobule 1-3\color{black})', '(\color{red}Lobule 4-6\color{black})');
title('Lipid Droplet Count');
Laura
Laura 2023-5-20
@Steven Lord I was trying to explain that if I try to change features to something else like features1. I get that error is there a way to fix that?

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by