Convert dat file into fprintf using strtok, strrep and char functions

1 次查看(过去 30 天)
I've used textscan to laod a dat file into matlab the file contains 100 lines and each line is in this format "string-001-1983-string" and i have to do the following: 1. Separate them into 3 columns so that "string-001" are in one, "1938" is in another and "string" is in the third (using strtok) 2. Then i need to remove the dash in the first column so that is read "string 001" (using strrep) 3. Finally i need to use fprintf to print them in a file with three columns and a header on each.
I have managed to use strtok to split it into 4 columns (separated at the dashes) but it's proving difficult to put the first two together again. If i do the following:
fileid = fopen('nem_skjal.dat');
if fileid == -1
disp('Error, file did not open');
else
disp('File opened successfully');
students = textscan(fileid,'%s');
% attempted to split at second dash but did not work
% [studid rest] = strtok(students,'-19')
[stud rest] = strtok(students,'-'); %stud = student
[id rest] = strtok(rest,'-'); % id = student number
[year rest] = strtok(rest,'-'); % year = birthyear
[sub rest] = strtok(rest,'-'); % sub = subject
stud_dash = strrep(stud,'i','i-')
charid=char(id{:});
charstud = char(stud_dash{:});
studid = strcat(charstud,charid);
% Failed attempts...
%stud_dash = strrep(stud,'i','i-')
%chars=char(studid{:});
%studid = [stud id];
%studid_dash = strrep(studid,'i','i-')
end
As you can see i have tried many different approaches but i get the error "Cell elements must be character arrays". That's my main error, but i get a whole bunch of others. Can anyone help me in finding the best way to do this?
Thanks in advance, Freyja

回答(1 个)

dpb
dpb 2013-11-3
This is a job for regexp()...
or just fix the input to fprintf()...
>> c={'string-001-1983-string'};
>> d=textscan(c{:},'%s%d%d%s','delimiter','-');
>> fprintf('%s %d,%d,%s\n',char(d{1}),d{2},d{3},char(d{4}))
string 1,1983,string
>>
  2 个评论
Freyja
Freyja 2013-11-4
Hi,
Thanks for replying! Unfortunately it didn't work for my data but i have worked out half the problem. I've now got 3 cell arrays, which i can use also as char arrays, however i do not know how to print them using fprintf. This is my attempt so far:
fileid = fopen('nem_skjal.dat');
if fileid == -1
disp('Error, file did not open');
else
disp('File opened successfully');
nemendur = textscan(fileid,'%s');
nemendur = nemendur{1,1};
[stud rest] = strtok(nemendur,'-'); %stud = nemendur
[id rest] = strtok(rest,'-'); % id = nemendar númer
[year rest] = strtok(rest,'-'); % year = fæðingaár
[fag rest] = strtok(rest,'-'); % fag = námsleið
studid = strcat(stud, '-',id);
studidcell = cellstr(studid);
studid_space = strrep(studidcell,'-',' ');
% Change them all to char
%studid_space = char(studid_space);
%year = char(year);
%fag = char(fag);
%fprintf('%s %d,%d,%s\n',char(d{1}),d{2},d{3},char(d{4}))
len = length(nemendur{1});
for i = 1:len
fprintf('%c%c%c\n',studid_space{1}(i)...
,year{1} (i),fag{1}(i))
end
end
But his gives me nothing useful.
dpb
dpb 2013-11-4
What does "didn't work" mean, specifically?
Give a sample of the input data and a desired output...

请先登录,再进行评论。

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by