I am getting this error

2 vues (au cours des 30 derniers jours)
Manav Divekar
Manav Divekar le 30 Jan 2022
Commenté : Manav Divekar le 31 Jan 2022
j = 1:length(fileNames)
thisFile = fileNames(j).name;
file_info = split(extractBefore(thisFile,'.txt'), "_");
block = file_info{2,1};
strainrate = file_info{3,1};
trial = file_info{5,1};
trialtxt = char(trial);
trialnum = trialtxt(2)-'0';
sheet = sprintf('%s_%s',char(block),char(strainrate));
if trialnum == 1
fig = fig+1;
else
fig = fig;
end
% for i = 1:6
i=1;
% Average Initial Length
dimensions(i,1) = readtable('BMES 442 642 Lab 2 Geometric Measurements(2).xlsx','Sheet',sheet,'Range',sprintf('B%d:B%d',cell,cell));
% Length After Test
area = (table2array(dimensions(:,3))./1000).*(table2array(dimensions(:,4))./1000);
readFile = importdata(fileNames(j).name);
fileData = readFile.data;
compressionDisp = fileData(:,1);
load = fileData(:,2);
time = fileData(:,3);
k = find(compressionDisp==0);
error:
Dot indexing is not supported for variables of this type.
Error in untitled2 (line 41)
fileData = readFile(j).data;
The readFile.data is working in b but not in a version how can i make it work in a version as well.

Réponses (2)

Cris LaPierre
Cris LaPierre le 30 Jan 2022
Modifié(e) : Cris LaPierre le 31 Jan 2022
You use importdata to create readFile, which returns data from the file as a matrix, multidimensional array, or scalar structure array, depending on the characteristics of the file. Perhaps the characteristics of your file are resulting in it returning a matrix instead of a structure?
You can read more about it here.
  4 commentaires
Manav Divekar
Manav Divekar le 31 Jan 2022
i am using txt files, there are more than just 3 files but cant share all as it will be too big
and this is my full code.
fileNames = dir('*.txt');
cell = 6;
cell2 = 8;
fig = 1;
for j = 1:length(fileNames)
thisFile = fileNames(j).name;
file_info = split(extractBefore(thisFile,'.txt'), "_");
block = file_info{2,1};
strainrate = file_info{3,1};
trial = file_info{5,1};
trialtxt = char(trial);
trialnum = trialtxt(2)-'0';
sheet = sprintf('%s_%s',char(block),char(strainrate));
if trialnum == 1
fig = fig+1;
else
fig = fig;
end
% for i = 1:6
i=1;
% Average Initial Length
dimensions(i,1) = readtable('BMES 442 642 Lab 2 Geometric Measurements(2).xlsx','Sheet',sheet,'Range',sprintf('B%d:B%d',cell,cell));
% Length After Test
dimensions(i,2) = readtable('BMES 442 642 Lab 2 Geometric Measurements(2).xlsx','Sheet',sheet,'Range',sprintf('B%d:B%d',cell2,cell2));
% Average Initial Width
dimensions(i,3) = readtable('BMES 442 642 Lab 2 Geometric Measurements(2).xlsx','Sheet',sheet,'Range',sprintf('C%d:C%d',cell,cell));
% Average Initial Thickness
dimensions(i,4) = readtable('BMES 442 642 Lab 2 Geometric Measurements(2).xlsx','Sheet',sheet,'Range',sprintf('D%d:D%d',cell,cell));
% Width After Test
dimensions(i,5) = readtable('BMES 442 642 Lab 2 Geometric Measurements(2).xlsx','Sheet',sheet,'Range',sprintf('C%d:C%d',cell2,cell2));
% Thickness After Test
dimensions(i,6) = readtable('BMES 442 642 Lab 2 Geometric Measurements(2).xlsx','Sheet',sheet,'Range',sprintf('D%d:D%d',cell2,cell2));
% end
area = (table2array(dimensions(:,3))./1000).*(table2array(dimensions(:,4))./1000);
readFile = importdata(fileNames(j).name);
fileData = readFile.data;
compressionDisp = fileData(:,1);
load = fileData(:,2);
time = fileData(:,3);
k = find(compressionDisp==0);
disp = compressionDisp(k(end):end);
newload = load(k(end):end);
figure(fig)
hold on
plot(disp,newload)
xlabel('Compression displacement (mm)')
ylabel('Load (N)')
title(sprintf('Load-Displacement of %s',sheet))
xlim([-5 0])
ylim([-10000 0])
stress = newload/area;
finalL = table2array(dimensions(:,2));
initialL = table2array(dimensions(:,1));
strain = finalL-initialL/initialL;
emod = abs(stress/strain);
avgEmod = mean(emod);
avgEmodMPA = avgEmod/1E+06;
cell = cell + 8*trialnum;
cell2 = cell2 + 8*trialnum;
eModTable{j,1} = block(1);
eModTable{j,2} = block(2);
eModTable{j,3} = strainrate;
eModTable{j,4} = trialnum;
eModTable{j,5} = avgEmodMPA;
end
Cris LaPierre
Cris LaPierre le 31 Jan 2022
With the files you have shared here, your code runs. Is there a different file that you are testing with that, when loaded, causes this error?
Note that the file names of the attached text files do not match the expected naming convention of your code. I had to append one more underscore at the start to get the code to run.
fileNames = dir('*.txt');
cell = 6;
cell2 = 8;
fig = 1;
for j = 1:length(fileNames)
thisFile = fileNames(j).name;
file_info = split(extractBefore(thisFile,'.txt'), "_");
block = file_info{2,1};
strainrate = file_info{3,1};
trial = file_info{5,1};
trialtxt = char(trial);
trialnum = trialtxt(2)-'0';
sheet = sprintf('%s_%s',char(block),char(strainrate));
if trialnum == 1
fig = fig+1;
else
fig = fig;
end
% for i = 1:6
i=1;
% Average Initial Length
dimensions(i,1) = readtable('BMES 442 642 Lab 2 Geometric Measurements(2).xlsx','Sheet',sheet,'Range',sprintf('B%d:B%d',cell,cell));
% Length After Test
dimensions(i,2) = readtable('BMES 442 642 Lab 2 Geometric Measurements(2).xlsx','Sheet',sheet,'Range',sprintf('B%d:B%d',cell2,cell2));
% Average Initial Width
dimensions(i,3) = readtable('BMES 442 642 Lab 2 Geometric Measurements(2).xlsx','Sheet',sheet,'Range',sprintf('C%d:C%d',cell,cell));
% Average Initial Thickness
dimensions(i,4) = readtable('BMES 442 642 Lab 2 Geometric Measurements(2).xlsx','Sheet',sheet,'Range',sprintf('D%d:D%d',cell,cell));
% Width After Test
dimensions(i,5) = readtable('BMES 442 642 Lab 2 Geometric Measurements(2).xlsx','Sheet',sheet,'Range',sprintf('C%d:C%d',cell2,cell2));
% Thickness After Test
dimensions(i,6) = readtable('BMES 442 642 Lab 2 Geometric Measurements(2).xlsx','Sheet',sheet,'Range',sprintf('D%d:D%d',cell2,cell2));
% end
area = (table2array(dimensions(:,3))./1000).*(table2array(dimensions(:,4))./1000);
readFile = importdata(fileNames(j).name);
fileData = readFile.data;
compressionDisp = fileData(:,1);
load = fileData(:,2);
time = fileData(:,3);
k = find(compressionDisp==0);
disp = compressionDisp(k(end):end);
newload = load(k(end):end);
figure(fig)
hold on
plot(disp,newload)
xlabel('Compression displacement (mm)')
ylabel('Load (N)')
title(sprintf('Load-Displacement of %s',sheet))
xlim([-5 0])
ylim([-10000 0])
stress = newload/area;
finalL = table2array(dimensions(:,2));
initialL = table2array(dimensions(:,1));
strain = finalL-initialL/initialL;
emod = abs(stress/strain);
avgEmod = mean(emod);
avgEmodMPA = avgEmod/1E+06;
cell = cell + 8*trialnum;
cell2 = cell2 + 8*trialnum;
eModTable{j,1} = block(1);
eModTable{j,2} = block(2);
eModTable{j,3} = strainrate;
eModTable{j,4} = trialnum;
eModTable{j,5} = avgEmodMPA;
end

Connectez-vous pour commenter.


Manav Divekar
Manav Divekar le 31 Jan 2022
  4 commentaires
Cris LaPierre
Cris LaPierre le 31 Jan 2022
I get no error for any of your files.
Somehow your version of readFile is different. Your code runs, so try to explore that. Clear your workspace (clear) and rerun your code. If you get the error still, run the following code in your command window and share the results.
whos readFile
fileNames(j).name
Attach the file identified by the 2nd line.
Note that, no matter what, your code is always loading the last file loaded. The index j is your for loop loop counter, which has already run, so j is equal to length(fileNames) at this point.
Manav Divekar
Manav Divekar le 31 Jan 2022
ok got it i will try that

Connectez-vous pour commenter.

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by