필터 지우기
필터 지우기

Problem with textscan for importng textfile

조회 수: 2 (최근 30일)
Rica
Rica 2017년 4월 18일
댓글: Rica 2017년 4월 19일
Hi, i many text files named Data. I attached as exemple one file to schow the structure of it. I wrote a function to read the Data of these files.
function [All]=myfunction(numoffiles)
for k=1:numoffiles
fid=fopen(['Data_' num2str(k) '.txt']);
if fid == -1, error('Cannot open file'),
end
XX{k,1}=textscan(fid,'%f %f %f');
fclose(fid)
Amplitude{k,1}=XX{k,1}{1};
Resonance{k,1}=XX{k,1}{2};
Height{k,1}=XX{k,1}{3};
end
All.Amplitude=vertcat( Amplitude{:});
All.Resonance=vertcat(Resonance{:});
All.Height=vertcat(Height{:});
end
when i run this function with matlab 2014, i does what it should. But with Matlab 2017 i get Pronblems. Could someone help to solve the matter. mybe there is an easier way to sove it.
  댓글 수: 2
Jan
Jan 2017년 4월 18일
Please explain the problems with details. Perhaps the files are not found or anything else happens. While you know already, what the problem is, we cannot even guess it without using a crystal ball.
Stephen23
Stephen23 2017년 4월 18일
dlmread might be a better choice for reading this file.

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2017년 4월 18일
I only had one file so I used a loop to read it twice to test the code.
This works in R2017a:
All.Amplitude = [];
All.Resonance = [];
All.Height = [];
for k1 = 1:2
fidi = fopen('Rica Data_2.txt','rt');
XXc = textscan(fidi, '%f%f%f', 'CollectOutput',1);
fclose(fidi);
XX = cell2mat(XXc);
Amplitude = XX(:,1);
Resonance = XX(:,2);
Height = XX(:,3);
All.Amplitude = [All.Amplitude; Amplitude];
All.Resonance = [All.Resonance; Resonance];
All.Height = [All.Height; Height];
end
  댓글 수: 1
Rica
Rica 2017년 4월 19일
Thank you for you answer. That is what am looking for. it does function with the data_2.txt which i attached. But when i use it for the real Data_2(large data) it does not work as i expect. I attached the Data_2.txt. thank you

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by