Problem: Create structure with a function using textfile and strtok(file,delimiter)

% 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!

Réponses (1)

Sry, i forgot to delete y=length(text). I tried a different solution with matrices and needed this variable.
I deleted this one since it does not bekong to this solution with blocks but its still not working and i get the same error :(
thx btw for trying!
EDIT: I corrected my question and now its easier to read, sry
EDIT2: Ok, I still dont know why i get [1x3 chars] in the while loop but i fixed the issue with direct acces to the cell elemts. Well thats not the solution I wanted but it works when u know ur file structure. Here is the fixed code:
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
A = struct(B{1},B{2},B{3}(1,3),B{4},B{5}(1,3),B{6});
save ignore A

Catégories

En savoir plus sur Data Type Identification dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by