read a file and covert it to two arrays

조회 수: 2 (최근 30일)
Robert Jones
Robert Jones 2019년 2월 22일
답변: Robert Jones 2019년 2월 23일
I have an ascii data file with the following content:
a=2
b=4
c=9
I need to read it and create two arrays: one containing the names of the variables and another containing the values of the variables, i.e.
vn=[a b c];
vv=[2 4 9];
I have no control on the input file, it's just given to me.
Any ideas?
Thanks
Robert
  댓글 수: 1
Gani
Gani 2019년 2월 22일
Can you upload a sample file ?

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

답변 (3개)

Robert Jones
Robert Jones 2019년 2월 22일
here it is
  댓글 수: 1
Image Analyst
Image Analyst 2019년 2월 22일
My answer below works with that just fine.

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


Image Analyst
Image Analyst 2019년 2월 22일
Try this (untested)
% Open the file.
fileID = fopen(fullFileName, 'rt');
loopCounter = 1;
% Read the first line of the file.
textLine = fgetl(fileID);
while ischar(textLine)
equalLocation = strfind(textLine, '=')
ca{loopCounter, 1} = textLine(1:(equalLocation-1));
ca{loopCounter, 2} = str2double(textLine((equalLocation+1):end))
% Read the remaining lines of the file.
fprintf('%s\n', textLine);
% Read the next line.
textLine = fgetl(fileID);
loopCounter = loopCounter + 1;
end
% All done reading all lines, so close the file.
fclose(fileID);
ca is your cell array output.
  댓글 수: 4
Robert Jones
Robert Jones 2019년 2월 22일
I see,
this how I solved the problem, not sure if this is the most elegant, but vn=[a b c] and qq=[5,3,9]
Thank you
clc
clear all
fullFileName='data.txt';
% Open the file.
fileID = fopen(fullFileName, 'rt');
loopCounter = 1;
% Read the first line of the file.
textLine = fgetl(fileID);
while ischar(textLine)
equalLocation = strfind(textLine, '=');
ca{loopCounter, 1} = textLine(1:(equalLocation-1));
ca{loopCounter, 2} = textLine((equalLocation+1):end);
% Read the remaining lines of the file.
fprintf('%s\n', textLine);
% Read the next line.
textLine = fgetl(fileID);
loopCounter = loopCounter + 1;
end
% All done reading all lines, so close the file.
fclose(fileID);
vn=char(ca{:,1});
vv=char(ca{:,2});
for i=1:size(vv)
qq(i)=str2double(vv(i));
end
Image Analyst
Image Analyst 2019년 2월 23일
You're right. Not elegant. No need at all for a second loop. If you want a numerical/double vector, qq, then just simply put qq into the loop instead of ca{loopCounter, 2}:
qq(loopCounter) = str2double(textLine((equalLocation+1):end))
And the reason I used a cell array instead of a character array is to be robust enough to handle possible cases where the letters before the equals sign are more than one, like a two letter variable instead of a one letter variable. If you know for a fact that you will ALWAYS have single letters then you can just put your vn into the loop along with the qq. Again, just a single for loop is needed, NOT two.

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


Robert Jones
Robert Jones 2019년 2월 23일
Thank you

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by