Problem: Create structure with a function using textfile and strtok(file,delimiter)
Show older comments
% 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!
Answers (1)
Siamak Arify
on 5 Mar 2015
Edited: Siamak Arify
on 5 Mar 2015
Categories
Find more on String Parsing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!