Reading unformatted text and converting to formatted

조회 수: 22 (최근 30일)
Ibro Tutic
Ibro Tutic 2015년 11월 24일
댓글: Rena Berman 2021년 5월 6일
Hi,
I have some files that contain raw text that is unformatted. These files have lots of lines and all of these lines are along the lines of
RFCH[0][0]:1, RFCH[0][1]:2, RFCH[0][2]:3,
etc. What I need to do is assign the value to the right of the colon to a corresponding matrix location, so the first 1 would go to [0,0] ([1,1] since Matlab probably wouldn't recognize [0,0]).
I know I can open the file using
fid=fopen(projectdir, 'r')
T=textscan(fid, '%s')
fclose(fid)
I am not really sure how go about this. Any help would be great.
  댓글 수: 4
Rena Berman
Rena Berman 2021년 5월 6일
(Answers Dev) Restored edit

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

채택된 답변

Stephen23
Stephen23 2015년 11월 24일
편집: Stephen23 2015년 11월 24일
You can read the whole file using fileread, identify the digits using regexp, convert the values to linear indices using sub2ind, and then simple linear indexing is all that is required to allocate the values from the file to the final matrix:
str = fileread('temp.txt');
% identify digits:
rgx = '[A-Z]+\[(\d+)\]\[(\d+)\]: *(\d+)';
C = regexp(str,rgx,'tokens');
% convert digits to numeric:
M = cellfun(@str2double,vertcat(C{:}));
M(:,1:2) = 1+M(:,1:2);
% convert to linear indices:
out = nan(max(M(:,1)),max(M(:,2)));
idx = sub2ind(size(out),M(:,1),M(:,2));
% allocate values:
out(idx) = M(:,3)
Because you did not provide any sample datafile I had to create my own, which contains only this string:
RFCH[0][0]:1, RFCH[0][1]:2, RFCH[0][2]:3, RFCH[2][2]:4
The code's output using my sample data file is this:
out =
1 2 3
NaN NaN NaN
NaN NaN 4
  댓글 수: 4
Ibro Tutic
Ibro Tutic 2015년 11월 24일
The file can't be uploaded due to NDA's and what not. And I figured that I could figure out what portion of the code looked at the letters and edit it in myself, but the method you use was something I did not think of and am not familiar with, so it didn't work out the way I intended.
Ibro Tutic
Ibro Tutic 2015년 11월 24일
편집: Ibro Tutic 2015년 11월 24일
I am getting the same error as above, it seems the matrix C is an empty 0x0 matrix, which I assume is causing the error. The space after the colon was an error on my part, there is not supposed to be a space. I modified your script to take this into account and I am still receiving the same error.

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

추가 답변 (1개)

dpb
dpb 2015년 11월 24일
RFCH[0][0]:1, RFCH[0][1]:2, RFCH[0][2]:3,
fmt='RFCH[%d][%d]:%d';
fid=fopen('yorfile');
c=cell2mat(textscan(fid,fmt,'delimiter',',','collectoutput',1));
fid=fclose(fid);
X(c(:,1)+1,c(:,2)+1)=c(:,3);
  댓글 수: 1
Stephen23
Stephen23 2015년 11월 24일
Subscripted assignment dimension mismatch.
Error in temp (line 5)
X(c(:,1)+1,c(:,2)+1) = c(:,3);

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by