이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
How to adjust the "isnan" to make it work for cell?
조회 수: 1 (최근 30일)
이전 댓글 표시
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
채택된 답변
Stephen23
2018년 2월 20일
편집: Stephen23
2018년 2월 20일
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
Ismail Qeshta
2018년 2월 20일
편집: Ismail Qeshta
2018년 2월 20일
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
2018년 2월 20일
편집: Stephen23
2018년 2월 20일
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
2018년 2월 20일
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
2018년 2월 20일
편집: Stephen23
2018년 2월 20일
"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
2018년 2월 20일
편집: Ismail Qeshta
2018년 2월 20일
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
2018년 2월 20일
편집: Ismail Qeshta
2018년 2월 20일
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
2018년 2월 20일
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
2018년 2월 20일
편집: Stephen23
2018년 2월 20일
@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
2018년 2월 20일
Many thanks Stephen. I think the data in the output files are over-written and I only see one row.
Stephen23
2018년 2월 20일
편집: Stephen23
2018년 2월 20일
@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
2018년 2월 20일
편집: Ismail Qeshta
2018년 2월 20일
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
2018년 2월 20일
편집: Stephen23
2018년 2월 20일
"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
2018년 2월 20일
편집: Ismail Qeshta
2018년 2월 20일
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
2018년 2월 20일
편집: Stephen23
2018년 2월 20일
"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
2018년 2월 20일
편집: Ismail Qeshta
2018년 2월 20일
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
2018년 2월 20일
편집: Ismail Qeshta
2018년 2월 20일
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
2018년 2월 20일
"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
2018년 2월 21일
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
2018년 2월 21일
"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
2018년 2월 21일
편집: Ismail Qeshta
2018년 2월 21일
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
2018년 2월 21일
@Ismail Qeshta: I hope that it was interesting. Good luck with the development, and come back if you have more questions!
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- 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)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)