0

I am developing a user interface using matlab wich allows to browse and load a text file and display some curves. I am facing a problem, my file text is a set of decimal number, matlab is reading those number as two columns. this is an exemple: u find here the file that I am working on:

enter image description here

After runing this code :

[filename pathname] = uigetfile({'*.txt'},'File Selector');
fullpathname = strcat(pathname,filename);
text = fileread(fullpathname); %reading information inside a file
set(handles.text6,'string',fullpathname)%showing full path name
set(handles.text7,'string',text)%showing information
loaddata = fullfile(pathname,filename);
xy = load(loaddata,'-ascii','%s');
t = xy(:,1);
i = xy(:,3);
handles.input1 = i;
handles.input2 = t;
axes(handles.axes1);
plot(handles.input1,handles.input2)

the curves looks so strenge, so I checked the result of xy= load(loaddata,'-ascii') using command window and here the problem appears!

enter image description here

So I have now 12 columns instead of 6 ! can u help me please? I tried with strrep(data,',','.') but it doesnt work !

Luka Kerr
  • 4,161
  • 7
  • 39
  • 50
  • 1
    So your question is about parsing the file? How about you post the *actual* file contents and show only the relevant lines of code. This has nothing to do with a GUI. – Suever Oct 11 '16 at 13:59
  • Hello, I edited my question now to make it more clear u can check it please :) – Emna Ameur Oct 11 '16 at 14:18
  • Can you paste the actual data into the question rather than posting a screenshot? – Suever Oct 11 '16 at 14:19
  • Possible duplicate of [Specify decimal separator for .dat file in matlab](http://stackoverflow.com/questions/20555743/specify-decimal-separator-for-dat-file-in-matlab) – sco1 Oct 11 '16 at 14:53

1 Answers1

0

Since you are using commas, for your radix point, you will want to first load in the entire file as a string, replace the , with . and then you can use str2num to convert the entire file to a numeric array

% Read the entire file into memory
fid = fopen(loaddata, 'rb');
contents = fread(fid, '*char')';
fclose(fid);

% Replace `,` with `.`
contents = strrep(contents, ',', '.');

% Now convert to numbers
data = str2num(contents);
Suever
  • 64,497
  • 14
  • 82
  • 101