Error in reading text file and implemeting conditional logic to it

2 次查看(过去 30 天)
I have 3 colums in my text file.The first 2 are numbers but the last one is mixed.I want to load them and then i want to process the third column so that when there is a variable 'any' it will show logic 1 and when there is some other number it will show a logic 0.In this way I want to create a matrix of 1 and 0.
The problem is I am not getting the roper values after using strcmp and the third column is not working properly. My code:
r=tdfread('Dummy.txt');
for rname = fieldnames(r)
things = r.(rname{3});
s1 = 'any';
for i=1:size(str,1)
a=str(i);
TF = strcmp(s1,a);
end
end
TF is only returning o which should not be the case.
I have attached the text file below

回答(1 个)

Guillaume
Guillaume 2019-4-30
编辑:Guillaume 2019-4-30
I don't have the stats toolbox and so have never used tdfread. You file can be easily read by readtable which doesn't require any toolbox:
t = readtable('ABC.txt');
t.C = strcmp(t.C, 'any') %change variable C into logical true if equal to 'any', false otherwise
edit: I've only just looked at the code you'd written. It's complete nonsense. This is what it should have been:
r=tdfread('Dummy.txt');
rname = fieldnames(r)
things = {r.(rname{3})};
TF = strcmp(things, 'any');
I'd still use tables for the job.

类别

Help CenterFile Exchange 中查找有关 Large Files and Big Data 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by