Cell contents reference from a non-cell array object from a txt file

1 visualización (últimos 30 días)
Hi, can you help me I am trying to run a function however gets stuck when I want it to open & read from my txt files where the 1st column is Iaa, 2nd errAA and so on ...
function [results] = kZ(Iaa,errAA,Ibb,errBB,time)
for k = 1:4
textFileName = ['A' num2str(k) '.txt'];
if exist(textFileName, 'file')
fid = fopen(textFileName, 'rt');
mydata = fread(fid);
fclose(fid);
else
fprintf('File %s does not exist.\n', textFileName);
end
end
Iaa = mydata{k}(:,1);
errAA = mydata{k}(:,2);
Ibb = mydata{k}(:,3);
errBB = mydata{k}(:,4);
time = mydata{k}(:,5);
ABsub = Iab - Iab(1);
BAsub = Iba - Iba(1);
[AAcol,AArow] = size(normAA);
if AArow > AAcol
normAA = normAA';
end
[BBcol,BBrow] = size(normBB);
if BBrow > BBcol
normBB = normBB';
end
[ABcol,ABrow] = size(normAB);
if ABrow > ABcol
normAB = normAB';
end
[BAcol,BArow] = size(normBA);
if BArow > BAcol
normBA = normBA';
end
  3 comentarios
Angelo Figueiredo
Angelo Figueiredo el 19 de Feb. de 2019
Editada: per isakson el 19 de Feb. de 2019
Cheers, Bob!
Error in kex_ZZ (line 12)
Iaa = mydata{k}(:,1);
per isakson
per isakson el 19 de Feb. de 2019
Editada: per isakson el 19 de Feb. de 2019
First, see fread, Read data from binary file. I assume that fread doesn't read what you expect.
Secondly, mydata = fread(fid); overwrites mydata for each file, but the first.

Iniciar sesión para comentar.

Respuestas (1)

Guillaume
Guillaume el 20 de Feb. de 2019
I would suspect that it is a text file and that your code was meant to use textscan instead of fread. textscan would return a cell array, so the rest of your code would make sense. fread will never return a cell array, so indeed you will get an error with the code you're using, it doesn't make any sense. You can't willy nilly replace a function with another and expect they work the same.
I would recommend that you give an example of the file you're trying to import. Most likely, the import can be done in just one line with readtable. Possibly:
function results = kZ(whataretheinputs) %see comment below regarding inputs. Giving the function a meaningful name would be a good idea as well
for fileindex = 1:4
textFileName = sprintf('A%d.txt', fileindex);
if exist(textFileName, 'file')
mydata = readtable(textFileName, 'VariableNames', {'Iaa', 'errAA', 'Ibb', 'errBB', 'time'}); %these are poor variable names.
else
fprintf('File %s does not exist.\n', textFileName);
continue; %skip rest of loop since nothing has been imported
end
%... rest of code
end
end
You also need to clarify what are the inputs and outputs of your function, because once again your code doesn't make sense. You specify Iaa, errAA, Ibb, errBB and time as inputs to your function but the first thing your function does is overwrite these inputs. I suspect they may be outputs instead. You specify results as an output instead but never assign anything to it.
  3 comentarios
Guillaume
Guillaume el 20 de Feb. de 2019
Your function still has the same issues that I and others have pointed out:
  • Why are Iaa, errAA, Ibb, errBB and time inputs to the function? These inputs are never used and are immediately overwritten by your function.
  • Your loop is reading 4 different files, each time storing what is read in the same array, overwriting the content that was read in the previous file. At the end of the loop, in data you only have the content of the last file. What is the actual intent?
Then you calculate some things that are never used (ABsub, BAsub, normErrxx), check if some vectors are columns vectors the complicated way (iscolumn would be simpler) when you are guaranteed they'll be column vectors (since you created them as x(:, col).
Maybe the start of your function should be:
function results = kZ() %sfunction with no inputs
for fileindex = 1:4
textFileName = sprintf('A%d.txt', fileindex);
if exist(textFileName, 'file')
mydata = readtable(textFileName, 'VariableNames', {'Iaa', 'errAA', 'Ibb', 'errBB', 'Iab', 'errAB', 'Iba', 'errBA', 'time'}); %these are poor variable names.
else
fprintf('File %s does not exist.\n', textFileName);
continue; %skip rest of loop since nothing has been imported
end
%... do something with data INSIDE THE LOOP. AFTER THE LOOP data is only the content of the last file
end
end
Also note that having variable names that only differ by case is really asking for trouble. Do not use both Iaa and IAA, you can be sure that at some point you'll be using one when you meant the other and then waste time trying to understand why you don't get correct results. All your variable names and function names are generally poor. They don't mean anything to the casual reader. Prefer full word that explain what the variable holds / function does.
Angelo Figueiredo
Angelo Figueiredo el 20 de Feb. de 2019
Thanks. This is a long function as you can see attached. It works perfectly calling file, by file but I wouldn't like to call 100 or more *.txt files individually. The purposes of asking help was really if somebody could help me out to solve this loop issue and get finally 100 plots and 100 outputs files. Saying that I haven't still been succesfully in this task. And now
"Error using readtable (line 143)
Invalid parameter name: VariableNames.
Error in kZ (line 10)
mydata = readtable(textFileName, 'VariableNames', {'Iaa', 'errAA', 'Ibb', 'errBB', 'Iab',
'errAB', 'Iba', 'errBA', 'time'});"
function [results] = kZ()
for fileindex = 1:4
textFileName = sprintf('A%d.txt', fileindex);
if exist(textFileName, 'file')
mydata = readtable(textFileName, 'VariableNames', {'Iaa', 'errAA', 'Ibb', 'errBB', 'Iab', 'errAB', 'Iba', 'errBA', 'time'});
else
fprintf('File %s does not exist.\n', textFileName);
continue; %skip rest of loop since nothing has been imported
end
Iaa = mydata{k}(:,1);
errAA = mydata{k}(:,2);
Ibb = mydata{k}(:,3);
errBB = mydata{k}(:,4);
Iab = mydata{k}(:,5);
errAB = mydata{k}(:,6);
Iba = mydata{k}(:,7);
errBA = mydata{k}(:,8);
time = mydata{k}(:,9);
ABsub = Iab - Iab(1);
BAsub = Iba - Iba(1);
total_intensity = Iaa(1) + Ibb(1);
normAA = Iaa/total_intensity;
normBB = Ibb/total_intensity;
normAB = ABsub/total_intensity;
normBA = BAsub/total_intensity;
normErrAA = (errAA./Iaa) .* normAA;
normErrBB = (errBB./Ibb) .* normBB;
normErrAB = (errAB./Iab) .* normAB;
normErrBA = (errBA./Iba) .* normBA;
Err2AA = errAA.^2;
Err2BB = errBB.^2;
Err2AB = errAB.^2;
Err2BA = errBA.^2;
Errfin = (Err2AA + Err2BB + Err2AB + Err2BA);
[AAcol,AArow] = size(normAA);
if AArow > AAcol
normAA = normAA';
end
[BBcol,BBrow] = size(normBB);
if BBrow > BBcol
normBB = normBB';
end
[ABcol,ABrow] = size(normAB);
if ABrow > ABcol
normAB = normAB';
end
[BAcol,BArow] = size(normBA);
if BArow > BAcol
normBA = normBA';
end
[default.kex] = 3.2;
[default.pA] = 0.5;
[default.R1A] = 1;
[default.R1B] = 1;
default = [default.kex;default.pA;default.R1A;default.R1B];
[fitparameters] = fminsearch(@(x) zex(x,normAA,normBB,normAB,normBA,time),default);
kex = fitparameters(1);
pA = fitparameters(2);
pB = 1 - pA;
R1A = fitparameters(3);
R1B = fitparameters(4);
[residual,IAA,IBB,IAB,IBA] = zex(fitparameters,normAA,normBB,normAB,normBA,time);
[results.kex] = kex;
[results.pA] = pA;
[results.pB] = pB;
[results.R1A] = R1A;
[results.R1B] = R1B;
results.datafit = [normAA normBB normAB normBA IAA IBB IAB IBA];
[results.dataIAA] = normAA;
[results.dataIBB] = normBB;
[results.dataIAB] = normAB;
[results.dataIBA] = normBA;
[results.fitIAA] = IAA;
[results.fitIBB] = IBB;
[results.fitIAB] = IAB;
[results.fitIBA] = IBA;
[results.residual] = residual./Errfin;
[results.k1] = kex * pA;
[results.k_1] = kex * pB;
end
function [residual,IAA,IBB,IAB,IBA] = zex(default,Iaa,Ibb,Iab,Iba,time)
kex = default(1);
pA = default(2);
pB = 1 - pA;
R1A = default(3);
R1B = default(4);
number_times = length(time);
aAA = zeros(number_times,1);
aBB = zeros(number_times,1);
aAB = zeros(number_times,1);
aBA = zeros(number_times,1);
for n = 1:1:number_times
aAA(n) = pA*(pA+pB*exp(-kex*time(n)))*exp(-R1A*time(n));
aBB(n) = pB*(pB+pA*exp(-kex*time(n)))*exp(-R1B*time(n));
aAB(n) = pA*pB*(1-exp(-kex*time(n)))*exp(-R1A*time(n));
aAB(n) = pA*pB*(1-exp(-kex*time(n)))*exp(-R1B*time(n));
end
IAA = pA*aAA;
IBB = pB*aBB;
IAB = pB*aAB;
IBA = pA*aBA;
deltaAA = Iaa - IAA;
deltaBB = Ibb - IBB;
deltaAB = Iab - IAB;
deltaBA = Iba - IBA;
residualAA = sum(deltaAA.^2);
residualBB = sum(deltaBB.^2);
residualAB = sum(deltaAB.^2);
residualBA = sum(deltaBA.^2);
residual = (residualAA + residualBB + residualAB +residualBA);
end
end

Iniciar sesión para comentar.

Categorías

Más información sobre MATLAB en Help Center y File Exchange.

Productos


Versión

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by