I have some code that keeps failing to open and read two csv files out of four. Can someone explain what is wrong with either the code or the csv files
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have a piece of code that plots force extension, stress strain and then the average stress strain of four csv files. I have used this multiple times on similar data sets but for some reason this code doesn't like all of the samples this time.
Here is the code
My m. file is called post process
%%
%3M Conventional-Consolodation Two-Stage-Sintering
%
%%
clear
close all
load('D:\Axiom\3M_CS\OXmcdata.mat')
%
Nsamples = 4;
%
L = 90; % coupon gauge section (mm)
A = [17.9 18.75 16.62 13.59]; % coupon cross-sectional area (mm^2)
%
figure(1);
hold;
figure(2);
hold
figure(3);
hold
%
for k = 1:Nsamples
%
file_name = ['Sample' num2str(k) '.csv'];
%
T = readtable(file_name,'VariableNamingRule','preserve');
%
[test(k).disp,test(k).force,test(k).eyy] = post_fun(T);
%
figure(1)
h(k) = plot(test(k).disp,test(k).force/1000);
set(h(k),'LineWidth',2);
leg_string{k} = ['Sample ' num2str(k)];
%
figure(3)
g(k) = plot(test(k).eyy*100,test(k).force/A(k));
set(g(k),'LineWidth',2);
%
leg_string{k} = ['Sample ' num2str(k)];
end
%
eyymax = max(test(1).eyy);
eyymin = min(test(1).eyy);
for k = 2:Nsamples
%
auxmax = max(test(k).eyy);
auxmin = min(test(k).eyy);
%
eyymax = min([eyymax auxmax]);
eyymin = max([eyymin auxmin]);
end
eyysim = linspace(eyymin,eyymax,21)';
%
figure(2)
for k = 1:Nsamples
test(k).stress = test(k).force/A(k);
P(k,1:2) = polyfit(test(k).eyy*100,test(k).stress,1);
stresslin(:,k) = polyval(P(k,:),eyysim*100);
end
meanstress = mean(stresslin,2);
stdstress = std(stresslin,0,2);
%
%
% g(k) = plot(eyysim*100,);
% set(g(k),'LineWidth',2);
% %
%
figure(1)
set(gcf,'Position',[200 200 1024 768],'Color',[1 1 1]);
set(gca,'FontSize',24);
xlabel('Displacement (mm)','FontSize',32,'Interpreter','latex');
ylabel('force (kN)','FontSize',32,'Interpreter','latex');
box on
grid on
legend(leg_string,'FontSize',28,'Location','northwest','Interpreter','latex');
%
%
figure(2)
set(gcf,'Position',[200 200 1024 768],'Color',[1 1 1]);
set(gca,'FontSize',24);
xlabel('Strain (\%)','FontSize',32,'Interpreter','latex');
ylabel('Stress (MPa)','FontSize',32,'Interpreter','latex');
plot(eyysim*100,meanstress,'k','LineWidth',2)
plot(eyysim*100,meanstress+stdstress,'k--','LineWidth',2)
plot(eyysim*100,meanstress-stdstress,'k--','LineWidth',2)
plot(OXmcdata(:,1),OXmcdata(:,2),'DisplayName','Farhandi et al (2021)','LineWidth',3,'Color','r','LineStyle','--');
title({'Graph showing the mean stress/strain behaviour of' ...
'3M spread tow manufactured using consolodation at $200^\circ C$'...
'for 2 hours and sintering at $1150^\circ C$ for four hours'},'Interpreter','latex')
box on
grid on
%legend(leg_string,'FontSize',28,'Location','northwest','Interpreter','latex');
%
figure(3)
set(gcf,'Position',[200 200 1024 768],'Color',[1 1 1]);
set(gca,'FontSize',24);
xlabel('Strain (\%)','FontSize',32,'Interpreter','latex');
ylabel('Stress (MPa)','FontSize',32,'Interpreter','latex');
plot(OXmcdata(:,1),OXmcdata(:,2),'DisplayName','Farhandi et al (2021)','LineWidth',3,'Color','r','LineStyle','--');
box on
grid on
legend(leg_string,'FontSize',28,'Interpreter','latex');
%
function [disp,force,eyy] = post_fun(T)
%
disp = table2array(T(:,4));
force = table2array(T(:,end));
eyy = table2array(T(:,10));
%
index = isnan(disp) | isnan(force) | isnan(eyy) | disp < 0 | force < 0 | eyy < 0;
%
disp(index) = [];
force(index) = [];
eyy(index) = [];
%
[~,index] = max(force);
disp(index+1:end) = [];
force(index+1:end) = [];
eyy(index+1:end) = [];
%
end
Please hel me figure out what is wrong.
% Unable to perform assignment because the left and right sides have a different number of elements.
%
% Error in post_process (line 31)
% h(k) = plot(test(k).disp,test(k).force/1000);
This is the error
I don't really understand Matlab errors so it is quite difficult to figure out.
Thanks
Alex
0 commentaires
Réponse acceptée
Walter Roberson
le 29 Août 2024
Replace
h(k) = plot(test(k).disp,test(k).force/1000);
set(h(k),'LineWidth',2);
with
h{k} = plot(test(k).disp,test(k).force/1000, 'LineWidth', 2);
and replace
g(k) = plot(test(k).eyy*100,test(k).force/A(k));
set(g(k),'LineWidth',2);
with
g{k} = plot(test(k).eyy*100,test(k).force/A(k), 'LineWidth', 2);
0 commentaires
Plus de réponses (1)
Voss
le 28 Août 2024
When processing Sample3.csv, post_fun removes every row of table T because index, defined here
index = isnan(disp) | isnan(force) | isnan(eyy) | disp < 0 | force < 0 | eyy < 0;
is all true. That is, each row of T has a disp, force, and/or eyy value that is NaN or negative.
Here's running the relevant code just for that file to show that test(k) contains empty disp, force, and eyy fields.
k = 3;
file_name = ['Sample' num2str(k) '.csv'];
T = readtable(file_name,'VariableNamingRule','preserve');
[test(k).disp,test(k).force,test(k).eyy] = post_fun(T);
function [disp,force,eyy] = post_fun(T)
%
disp = table2array(T(:,4));
force = table2array(T(:,end));
eyy = table2array(T(:,10));
%
index = isnan(disp) | isnan(force) | isnan(eyy) | disp < 0 | force < 0 | eyy < 0;
%
disp(index) = [];
force(index) = [];
eyy(index) = [];
%
[~,index] = max(force);
disp(index+1:end) = [];
force(index+1:end) = [];
eyy(index+1:end) = [];
%
end
% disp, force, and eyy are all empty vectors (0-by-1):
disp(test(k))
% relevant columns of table T for reference:
disp(T(:,[4 end 10]))
When you plot some empty vectors you get zero lines:
temp = plot(test(k).disp,test(k).force/1000)
Which cannot be stored in h(k), since h(k) is a single element of h, i.e., enough to store exactly one line object.
h(k) = plot(test(k).disp,test(k).force/1000);
% ^^^^ h(k) is a slot for exactly one line, but
% ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this plot call returns zero lines
That's the reason for the error.
It's clear the code was written expecting that plot call to return a single line object, which is an assumption that is violated in this case. How should the situation that the plotted vectors are empty be handled? Maybe post_fun shouldn't be removing rows of T that contain negative disp, force, or eyy?
6 commentaires
Voss
le 30 Août 2024
For Sample3.csv, because post_fun has removed all the rows due to each row having a NaN or negative, test(k).disp, test(k).force, and test(k).eyy are all empty vectors (0-by-1):
disp(test(k))
disp: [0x1 double]
force: [0x1 double]
eyy: [0x1 double]
When you plot some empty vectors you get zero lines:
temp = plot(test(k).disp,test(k).force/1000)
temp =
0x1 empty Line array.
Which cannot be stored in h(k), since h(k) is a single element of h, i.e., enough to store exactly one line object. That's what the error message is trying to say.
Voir également
Catégories
En savoir plus sur Annotations dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!