You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to adjust the "isnan" to make it work for cell?
3 views (last 30 days)
Show older comments
Hi I have the below for loop. I would like to replace the NaN data output with 0.07. I keep getting the below error message. Could anyone please advise me how to stop the error message? Thank you.
for i=1:size(A,2)%number of columns
x{i} = interp1(y3, x3, A(:,i), 'linear');
k=1:1;
temp=x(k,:);
temp(isnan(temp))=0.07;
x(k,:)=temp;
fid=fopen(['result_' num2str(1) '.txt'],'w');
fprintf(fid,'%f\n',x);
fclose(fid);
end
The error message:
Undefined function 'isnan' for input arguments of type 'cell'.
Error in InterpolaeMFJavadCompleted (line 24)
temp(isnan(temp))=0.07;
1 Comment
Accepted Answer
Stephen23
on 20 Feb 2018
Edited: Stephen23
on 20 Feb 2018
Don't waste your time with cell arrays and cell2mat and the like, you don't need them. Just use a numeric variable:
N = size(A,2) %number of columns
%C = cell(1,N);
for k = 1:N
tmp = interp1(y3, x3, A(:,k), 'linear');
tmp(isnan(tmp))=0.07;
%C{k} = tmp;
fnm = sprintf('result_%d.txt',k);
[fid,msg] = fopen(fnm,'wt');
assert(fid>=3,msg)
fprintf(fid,'%f\n',tmp);
fclose(fid);
end
I also made some other small changes to make your code more robust.
25 Comments
Ismail Qeshta
on 20 Feb 2018
Edited: Ismail Qeshta
on 20 Feb 2018
Many thanks Stephen. The results appear only in one column with 10 rows, while they should be 10 columns with 100 rows. I have been facing similar problem, which is the interpolation needs to be performed for 400 columns from 100 plots.
Stephen23
on 20 Feb 2018
Edited: Stephen23
on 20 Feb 2018
Get rid of the loop and just supply the matrix as an input:
tmp = interp1(y3, x3, A, 'linear');
tmp(isnan(tmp)) = 0.07;
I have no idea what size A is, but if you want multiple columns then you will need to change that format string to specify that, e.g.:
fmt = repmat('%f',1,size(tmp,2));
fmt = [fmt,'\n'];
and you might want to transpose the matrix to get the same order in the file as you see in the matrix:
[fid,msg] = fopen(fnm,'wt');
assert(fid>=3,msg)
fprintf(fid,fmt,tmp.');
fclose(fid);
Ismail Qeshta
on 20 Feb 2018
Many thanks Stephen. Actually, for results processing and plotting, I will need to separate the output files into 400 file each corresponds to one column.
Stephen23
on 20 Feb 2018
Edited: Stephen23
on 20 Feb 2018
"The results appear only in one column with 10 rows, while they should be 10 columns with 100 rows"
If A has 10 rows then your code will produce a 10 row output vector, which is apparently exactly what you get:
"The results appear only in one column with 10 rows"
So how do you expect to get 1000 interpolated values:
"while they should be 10 columns with 100 rows"
by only sampling the data at 10 locations?
Ismail Qeshta
on 20 Feb 2018
Edited: Ismail Qeshta
on 20 Feb 2018
Thanks Stephen for addressing my questions. I mean each interpolation will cover one column from A over 100 plots. So the results will be 100 (rows) X 10 (columns). This needs to be printed in one file. Since A has 400 columns, then I will need to have 400 files.
Ismail Qeshta
on 20 Feb 2018
Edited: Ismail Qeshta
on 20 Feb 2018
It does not have to be exactly 100 (rows) X 10 (columns), it could be for example one row of 1000 results, as long as I have the output of each column in A over the 100 plots in separate files.
Ismail Qeshta
on 20 Feb 2018
I am using the following code for the plots:
clear; clc;
Folder = cd;
N=100;
for k = 1:N;
Driftt = sprintf('Drift%d.out', k);
Reactt = sprintf('React%d.out', k);
matDrift = importdata(fullfile(Folder, Driftt));
matReact = importdata(fullfile(Folder, Reactt));
x1= matDrift(:,2);
y1= -sum(matReact(:,2:11),2);
[x3, ix] = unique(x1);
y3 = y1(ix);
Stephen23
on 20 Feb 2018
Edited: Stephen23
on 20 Feb 2018
@Ismail Qeshta: something like this?:
N = size(A,2) % number of columns
P = numel(??) % number of plots
for kn = 1:N
fnm = sprintf('result_%d.txt',kn);
[fid,msg] = fopen(fnm,'wt');
assert(fid>=3,msg)
for kp = 1:P
... whatever with plots
tmp = interp1(y3, x3, A(:,kn), 'linear');
tmp(isnan(tmp))=0.07;
fprintf(fid,'%f,',tmp);
fprintf(fid,'\n')
end
fclose(fid);
end
Ismail Qeshta
on 20 Feb 2018
Many thanks Stephen. I think the data in the output files are over-written and I only see one row.
Stephen23
on 20 Feb 2018
Edited: Stephen23
on 20 Feb 2018
@Ismail Qeshta: if you keep opening the files using w then each time the file data will be overwritten. If you want to append data to the file then you need to use a, as is clearly explained in the fopen documentation. However you will also have to make a special case for before the first iteration to empty the file of all data, so that you do not just keep expanding the files forever.
However that is not what I showed you, nor what I would recommend.
You could avoid all of this by opening each file once using wt, doing all of the "plot" processing, and then closing the file. This is likely to be more robust and simpler to manage.
Ismail Qeshta
on 20 Feb 2018
Edited: Ismail Qeshta
on 20 Feb 2018
Thanks Stephen for the explanation. I am sorry, I know I am taking too long to get the right code. I mean that the out put is only one column, while it should be 400 for each plot. Lets simplify it and take only one plot, from which we would like to interpolate the 400 columns. I would expect in this case to have 400 columns for this single plot. The code you provided above, however, only gives me one column.
Stephen23
on 20 Feb 2018
Edited: Stephen23
on 20 Feb 2018
"I would expect in this case to have 400 columns for this single plot. The code you provided above, however, only gives me one column."
Earlier you stated that the files "... will be 100 (rows) X 10 (columns). This needs to be printed in one file. Since A has 400 columns, then I will need to have 400 files."
So what do you want: 400 files with ten columns each, or 400 columns in XXX files? Why is this changing?
Perhaps you want something like this:
Nr = size(A,1) % number of rows
Nc = size(A,2) % number of columns
Np = numel(??) % number of plots
fmt = repmat(',%f',1,Nr);
fmt = [fmt(2:end),'\n'];
for kc = 1:Nc
fnm = sprintf('result_%d.txt',kc);
[fid,msg] = fopen(fnm,'wt');
assert(fid>=3,msg)
for kp = 1:Np
...
tmp = interp1(y3, x3, A(:,kc), 'linear');
tmp(isnan(tmp))=0.07;
fprintf(fid,fmt,tmp);
end
fclose(fid);
end
This will produce Nc=400 files, each with Nr=10 columns and Np=100 rows.
Ismail Qeshta
on 20 Feb 2018
Edited: Ismail Qeshta
on 20 Feb 2018
I cannot thank you enough Stephen for your great help.
Actually you are right, the code produces 400 file each has 10 column, but it has only one row.
I am using the following code that combines my plots and your provided code above.
clear; clc;
Folder = cd;
N=100;
for k = 1:N;
Driftt = sprintf('Drift%d.out', k);
Reactt = sprintf('React%d.out', k);
matDrift = importdata(fullfile(Folder, Driftt));
matReact = importdata(fullfile(Folder, Reactt));
x1= matDrift(:,2);
y1= -sum(matReact(:,2:11),2);
[x3, ix] = unique(x1);
y3 = y1(ix);
A=dlmread('AllPiers2.txt');
Nr = size(A,1) % number of rows
Nc = size(A,2) % number of columns
Np = numel(100) % number of plots
fmt = repmat(',%f',1,Nr);
fmt = [fmt(2:end),'\n'];
for kc = 1:Nc
fnm = sprintf('result_%d.txt',kc);
[fid,msg] = fopen(fnm,'wt');
assert(fid>=3,msg)
for kp = 1:Np
...
tmp = interp1(y3, x3, A(:,kc), 'linear');
tmp(isnan(tmp))=0.07;
fprintf(fid,fmt,tmp);
end
fclose(fid);
end
end
Stephen23
on 20 Feb 2018
Edited: Stephen23
on 20 Feb 2018
"Is it possible to ask how to make the row number 100?"
The row number of what?
If you mean that you want 100 rows in the output files then this is determined by Np, which you should set to 100. Currently you have it set to 1, because numel(100) is equal to 1, and is not equal to 100 (try it and see). So you need:
Np = 100;
Ismail Qeshta
on 20 Feb 2018
Edited: Ismail Qeshta
on 20 Feb 2018
Many thanks Stephen. You did a great help. I am sorry, I know I have taken much of your time. I need to use find command to find a value from all these files. My find command only works for files with one column. I am not sure if we can change the above code or my find command code. I have copied the find below for your kind reference. In other words, if we can have all the 1000 data in each file in one column with 1000 rows.
numfiles = 400;
threshold = 0.0186636;
linesabovethreshold = zeros(numfiles, 1);
for fileidx = 1:numfiles
filecontent = dlmread(sprintf('result_%d.txt', fileidx));
assert(iscolumn(filecontent), 'file number %d has more than one column', fileidx);
linesabovethreshold(fileidx) = sum(filecontent >= threshold); %only works if file has only one column
end
dlmwrite('Fragility_mm.txt', linesabovethreshold / 1000);
Ismail Qeshta
on 20 Feb 2018
Edited: Ismail Qeshta
on 20 Feb 2018
Also, I just did manual check using other command of find. Unfortunately, I could not obtain the same results. I would like to double-check if the code you provided interpolates each column in the 400 columns in file A from the 100 plots? Thank you again Stephen.
Stephen23
on 20 Feb 2018
"I would like to double-check if the code you provided interpolates each column in the 400 columns in file A from the 100 plots"
That is a good idea. You should never trust code that anyone gives you: always check it for yourself, and make sure that it does what you need it to do.
Ismail Qeshta
on 21 Feb 2018
Many thanks Stephen for your great time, effort and help. I really appreciate that.
I think after all, I should stick with my original code and make the isnan work for the cell array.
Stephen23
on 21 Feb 2018
"I think after all, I should stick with my original code and make the isnan work for the cell array."
Personally I wouldn't bother: don't make code more complex than it needs to be. Your code does not need a cell array to store that data.
Ismail Qeshta
on 21 Feb 2018
Edited: Ismail Qeshta
on 21 Feb 2018
I really would like to thank you again Stephen for your patience and great help. This morning, after going through your comments again to try to figure out the code that I need, I discovered the problem in my previous code, and got exactly what I need. Your response to my questions was simply awesome and professional. Thank you very much.
Stephen23
on 21 Feb 2018
@Ismail Qeshta: I hope that it was interesting. Good luck with the development, and come back if you have more questions!
More Answers (0)
See Also
Categories
Find more on Interactive Control and Callbacks in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)