Read text file in MATLAB

조회 수: 3 (최근 30일)
fendertunes
fendertunes 2016년 7월 3일
댓글: Azzi Abdelmalek 2016년 7월 3일
Hi,
I have a .txt file with lots of lines of text in the following format -
18400 8510.27+j8207.84
19200 1336.93+j3261.25
20000 14176.3+j11725.7
I would like to read this in matlab and plot it. How can I do it?
I have tried converting the .txt to .xlsx and using xlsread function -
[col1, col2] = xlsread('myfile.xlsx');
But the result is a double and a cell matrix. I can't treat cell as a number to plot.
Also the length of rows in the .txt file could vary (could be 100 rows, couple be 10,000 rows). Is there a way to read the numbers? I am ok with creating temporary variables, using repmat or doing str2num conversion. However, I haven't figured out a way to do it so far.

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2016년 7월 3일
편집: Azzi Abdelmalek 2016년 7월 3일
M=importdata('yourfile.txt')
a=regexp(M,'\S+','match');
b=strrep([a{:}],'j','j*')
c=str2double(b)
out=reshape(c,[],2)
  댓글 수: 2
fendertunes
fendertunes 2016년 7월 3일
Azzi, When I do it this way, I get the following error message: Error using dlmread (line 147)
Mismatch between file and format string.
Trouble reading 'Numeric' field from file (row number 2, field number 1) ==> +j0\n
Also, some rows have complex values saved as -
12000 15142.1+j-1037.75
12800 51708.5+j-4586.51
Maybe MATLAB has a problem interpreting this?
Azzi Abdelmalek
Azzi Abdelmalek 2016년 7월 3일
look at edited answer

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

추가 답변 (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