Problem: Create structure with a function using textfile and strtok(file,delimiter)
Mostrar comentarios más antiguos
% the code: read the strings of input file "Datei" and process them with strtok() and create a block with the tokens as elements. the content of the file is 2 colums of strings resp. matrices seperated with " " and ";".
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [A] = ignore(Datei)
fid = fopen(Datei,'r');
text = fscanf(fid,'%c');
fclose(fid);
R = text;
k = 1;
while (~isempty(R))
[T,R] = strtok(R,{' ',';'})
B{1,k} = T
k = k + 1;
end
iscellstr(B)
A = struct(B{1},B{2},B{3},B{4},B{5},B{6})
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
textfile content:
x [1,2,3]; y [9,4,5]; z [6,7,8]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
error:
Error using struct Invalid field name "
y"
Error in ignore (line 27) A = struct(B{1},B{2},B{3},B{4},B{5},B{6})
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
The problem is that the function strtok() (which i have to use according to the task) somehow makes the string "y" a [1x3 char] element which I can't handle.
I am new to Matlab so maybe this is just a scrub mistake. Anywas I hope to get an answer quite soon.
THX!
Respuestas (1)
Siamak Arify
el 5 de Mzo. de 2015
Editada: Siamak Arify
el 5 de Mzo. de 2015
Categorías
Más información sobre String Parsing en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!