Converting unformatted text to formatted text
Show older comments
I asked this question before and neglected some info, so I want to start fresh to avoid confusion.
clear all;
close all
clc
projectdir = 'C:\Users\me\data.psr';
newdir = 'C:\Users\me\Desktop\Test1';
fid=fopen(projectdir,'r');
T=textscan(fid, '%s');
fclose(fid);
for i=8:107
a=T{1,1}{i,1};
b= a(30:48);
matrix(i).r = b(2);
matrix(i).c = b(5);
matrix(i).info = b(8:13);
end
A = zeros(9,9)
for j=8:107
A(matrix(j).r, matrix(j).c) = matrix(j).info;
end;
The error:
Assignment has more non-singleton rhs dimensions than non-singleton subscripts
Error in Untitled2 (line 23)
A(matrix(j).r, matrix(j).c) = matrix(j).info;
This answer by user Stephen Cobeldick might help, although it was created only to deal with the histogram. It gives an error when ran however.
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)
Error using cellfun
Input #2 expected to be a cell array, was double instead.
Error in Untitled3 (line 12)
M = cellfun(@str2double,vertcat(C{:}));
7 Comments
@Ibro Tutic: you have edited your other question and removed all information from it, making our answers useless. Some might consider this to be extremely rude. On this forum it is certainly not considered to be helpful.
You have copied my code, but did not make the single small gesture of acknowledging this with a vote, or by accepting my answer, even though it perfectly resolved your original question.
Your comment that it "It gives an error when ran however" is incorrect: it works perfectly, without error, for the file that I had to create (because you did not provide any sample data).
Ibro Tutic
on 25 Nov 2015
Stephen23
on 25 Nov 2015
I am just pointing out that you are not the only person involved in this forum, and yet when you unilaterally decide to delete your question text it affects everyone who was involved, especially those who volunteered their time to develop valid answers. Imagine if everyone decided to delete their questions after they got an answer: this forum would be useless as a store of information available for everyone to use. That is all.
It is not just you, other users do it too. You might be interested to read what other volunteers feel about this behavior:
per isakson
on 25 Nov 2015
Stephen, Thanks for making me aware that I'm wasting my time!
Ibro Tutic
on 25 Nov 2015
Edited: John Kelly
on 10 Nov 2017
Stephen23
on 25 Nov 2015
I hope that you get the help and information that you need, and have fun learning MATLAB! We do put a lot of effort in when people need it, so please come and ask more questions :)
Stephen23
on 26 Dec 2020
OP deleted comments which are still visible in Google Cache:
Accepted Answer
More Answers (1)
dpb
on 24 Nov 2015
>> fmt='%*s%f%f%f';
>> fid=fopen('test4.txt');
>> c=cell2mat(textscan(fid,fmt,'headerlines',3,'delimiter','[]:','collectoutput',1,'multipledelimsAsOne',1));
>> v(sub2ind(sz,c(:,1)+1,c(:,2)+1))=c(:,3)
v =
Columns 1 through 10
1 0 1 1000 0 0 0 1 0 0
Columns 11 through 20
0 0 0 1 0 0 0 0 0 0
>> fid=fclose(fid);
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!