How to read vector from .RTF file into MATLAB?

21 次查看(过去 30 天)
Hi MATLAB Central,
I have a very simply question - I would like to read a single row vector (called "cells_QC") from an .rtf file into MATLAB. My RTF file contains only something like:
cells_QC = [2,4,5,6,8,10,12,14,15,16,17,18,19,20,21;];
In retrospect, I shouldn't have saved these vectors in .rtf. But now I have to parse through a bunch of folders and read them in. I attempted to use functions fopen, load and fscanf, but when read they just give a single number.
I would really appreciate any help with how to read this into MATLAB as a vector.
Thanks in advance,
Anders

采纳的回答

Anders
Anders 2014-1-31
After reading around, I actually found a solution. This might be not very elegant, but it works fine. I am just posting this in cases anyone else is interested. For "RTF_file path", give the file path for the RTF file.
fid = fopen(RTF_file_path);
%get each line in the file and run WHILE loop until you read the line with
%'cells_QC'
tline = fgetl(fid);
while ischar(tline)
desired_line = strfind(tline, 'cells_QC');
%If the desired line is found break the loop
if desired_line
break;
end
tline = fgetl(fid);
end
%Find beginning and end of the row_vector
vector_left = find(tline == '['); vector_right = find(tline == ']');
vector = tline(vector_left:vector_right);
%Convert from string to vector
cells_QC = str2num(vector);

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 String Parsing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by