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

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

采纳的回答

Walter Roberson
Walter Roberson 2024-8-29
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);

更多回答(1 个)

Voss
Voss 2024-8-28
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))
disp: [0x1 double] force: [0x1 double] eyy: [0x1 double]
% relevant columns of table T for reference:
disp(T(:,[4 end 10]))
Y-displacement (Strain Gauge 1) ForceADC1Channel1 Eyy - RC (Strain Gauge 1) _______________________________ _________________ _________________________ NaN NaN NaN 0 -436.1 2.22e-16 -0.00012479 -425.83 1.43e-05 0.0012639 -419.38 -4.62e-05 -0.00013617 -405.34 -3.35e-06 -0.00050161 -416.8 1.03e-05 0.0011603 -445.61 3.41e-06 -0.00063076 -392.68 5.07e-05 -0.0017988 -306.6 4.01e-05 -0.0069887 -201.1 0.00018296 -0.011494 -38.675 0.00024613 -0.019983 171.35 0.00035093 -0.027295 338.02 0.00044636 -0.034253 560.21 0.00057036 -0.042832 767.67 0.00068995 -0.051284 973.48 0.00080948 -0.060255 1191.9 0.00094657 -0.068178 1369.5 0.0010625 -0.076328 1547.9 0.001154 -0.083529 1746.9 0.0012349 -0.090759 1911.9 0.0013251 -0.10058 2106 0.0014168 -0.10835 2281.5 0.0015124 -0.11624 2440.9 0.0015719 -0.12422 2622.9 0.0017376 -0.13314 2763.6 0.0018018 -0.14089 2912.6 0.0018513 -0.14957 3067.6 0.0019891 -0.1579 3188.6 0.002053 -0.16572 3318.4 0.0020814 -0.17349 3417.8 0.0020724 -0.18165 3534.2 0.0021593 -0.19 3640.3 0.0022839 -0.19748 3739.1 0.0023523 -0.20667 3833.9 0.0023563 -0.2183 3932.5 0.0024687 -0.2241 4003.9 0.002477 -0.23367 4076.8 0.0024916 -0.24154 4142.2 0.0025482 -0.25153 4211.4 0.0025995 -0.26116 4283.4 0.002648 -0.26761 4327.6 0.0026228 -0.30327 3962.7 0.0023672 -0.44024 1284.9 0.00097759 -0.44791 1454.3 0.0010561 -0.45414 1652.2 0.0011356 -0.45803 1733.9 0.0012132 -0.46 1728 0.0011809 -0.45971 1721.9 0.0012358 -0.45913 1716.1 0.0011932
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.
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 个评论
Voss
Voss 2024-8-30
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by