MATLAB Answers

Reading unformatted text and converting to formatted

52 views (last 30 days)
This question was flagged by Stephen Cobeldick
Asked my question again with more relevant info to avoid confusion. Sorry and thanks for the help.
I have a text file (attached) with data that is similar to data I am looking at. I can't attach the actual file due to NDA's, but the file I attached is essentially the same with different numbers, the only part I care about it the RainflowCycleCounter stuff, the temp stuff is for other applications. What I need to do is take, for example, RainflowCycleCounterHistogram[0][0]=0, and map this value, 0, to a matrix location equal to [0,0].


Stephen Cobeldick
Stephen Cobeldick on 25 Nov 2015
@Ibro Tutic: please do NOT remove your question like this. We are volunteers who give our time to help you and everyone else who might be reading this forum. When you delete your question you make our answers meaningless and pointless, and you make our information useless for everyone else as well. You are telling us that our volunteered time is worthless, that our efforts are not important, and that other people shall not learn from our efforts.
You have just made the answer below worthless, even though several people have spent their own time volunteering their time to help both you and other browsers of this forum.
Ibro Tutic
Ibro Tutic on 25 Nov 2015
Notice I linked the other question. If somebody was interested in the solution and what it is referencing, they can go to that question to get ALL the relevant information. This question was worded extremely badly on my part and attempting to fix it by commenting and editing the original question would confuse everybody looking at it.

Sign in to comment.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 24 Nov 2015
Edited: Stephen Cobeldick on 24 Nov 2015
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 4


Show 1 older comment
Stephen Cobeldick
Stephen Cobeldick on 24 Nov 2015
Your original question did not have the space character after the colons, and my computer is not very good at reading your mind. I have now altered my answer to include your new information. If you have any other useful information about the strings, now is a good time to tell us. Even better is to simply upload the file itself so we can try our code on it. You can upload a file by clicking the paperclip button, and then pressing both the Choose file and Attach file buttons.
"The letters don't matter at all..." actually they do. As you can see dpb's answer uses exactly the literal string 'RFCH'. Every piece of information is important.
Ibro Tutic
Ibro Tutic on 24 Nov 2015
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 on 24 Nov 2015
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.

Sign in to comment.

More Answers (1)

dpb on 24 Nov 2015
RFCH[0][0]:1, RFCH[0][1]:2, RFCH[0][2]:3,

  1 Comment

Stephen Cobeldick
Stephen Cobeldick on 24 Nov 2015
Subscripted assignment dimension mismatch.
Error in temp (line 5)
X(c(:,1)+1,c(:,2)+1) = c(:,3);

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by