我有一个数据文件,其中包含参数名称和值,它们之间具有相同的符号。它是这样的:
select * from table_1 a
inner join table_2 b on a.field_1 = b.field_1
等号前后总有一个空格。问题是一些变量没有分配给它们的值,例如" C"以上,我需要把它们除掉。
我想将数据文件(文本)读入单元格,只删除带有这些无效语句的行,或者只创建一个没有它们的新数据文件。
无论哪个更容易,但我最终会使用textscan命令将文件读入一个单元格。
值(数字)将被视为双精度。
请帮助。
谢谢,
埃里克
答案 0 :(得分:0)
如果您只想获取变量,并拒绝没有任何字符的行,这可能会起作用(data.txt只是您给出的数据示例生成的txt):
fid = fopen('data.txt');
tline = fgets(fid);
while ischar(tline)
tmp = cell2mat(regexp(tline,'\=(.*)','match'));
b=str2double(tmp(2:end));
if ~isnan(b)
disp(b)
end
tline = fgets(fid);
end
fclose(fid);
我正在逐行读取txt文件,并使用通用表达式去除无用的字符,然后转换为读取值的两倍。
答案 1 :(得分:0)
试试这个:
fid = fopen('file.txt'); %// open file
x = textscan(fid, '%s', 'delimiter', '\n'); %// or '\r'. Read each line into a cell
fclose(fid); %// close file
x = x{1}; %// each cell of x contains a line of the file
ind = ~cellfun(@isempty, regexp(x, '=\s[\d\.]+$')); %// desired lines: space, numbers, end
x = x(ind); %// keep only those lines