how can i open my files?

hi everybody today i have one question too!! i use fopen function to open my Experiment datas. but the Name and Extension of files vary from each other. could you give me informations have can i read all files automatically.Inaddtion beginning of files Name differs from each other too. example: msa02_141121_mscc5_11.erg m4_cst_cc3_150206_01.e00 and here is my code too in order to read first file. As you can guess i have maybe more than 1000 files so i want to read them in Loop..
clear all
clc
delimiter=';' ;
headerLines=4
formatSpec1 = '%s%s%s%s%s%[^\n\r]'
daten=fopen('msa02_141121_mscc5_11.erg','r')
zeile = textscan(daten,formatSpec1,181,'delimiter',';','headerLines',4)
fclose(daten);

 Accepted Answer

Image Analyst
Image Analyst on 13 Feb 2015

0 votes

Use dir(), or sprintf() depending on what you know or don't know and how you want to do it. Code samples are in the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F

39 Comments

but my files are nor sequentially numbered
Then use the dir option, as Image Analyst suggested and as the Wiki also explains.
cemsi888
cemsi888 on 16 Feb 2015
Edited: cemsi888 on 16 Feb 2015
the Problem is different now. i can read the Name of files in my Folder but when i want to bring the files in to read data Loop, it gives me error. here are my codes
function loadData_Callback(hObject, eventdata, handles)
[name,path]=uigetfile({'*.erg';'*.e00'},'All Matlab Files','Multiselect','on')
c=length([name])
for k=1:c
C=name(k)
end
if isequal(name,0)|| isequal(path,0)
fileName=''
else
fileName=[path name]
end
function Read_Callback(hObject, eventdata, handles)
delimiter=';' ;
headerLines=4
formatSpec1 = '%s%s%s%s%s%[^\n\r]'
daten=fopen('C','r')
zeile = textscan(daten,formatSpec1,181,'delimiter',';','headerLines',4)
fclose(daten);
when i break my program before the first' for' Loop matlab gives me name as a cell Array which is really advantegeous for me. However when i continue my program it clears 'name' and i can not write a Loop and thats why i could not use textscan to read datas of Experiment results. what should i do now? Could you please help me?
Stephen23
Stephen23 on 16 Feb 2015
Edited: Stephen23 on 16 Feb 2015
You don't show us how you are calling these functions, so it is hard to know what is happening. But there are a few points to pay attention to:
  • Why are there two callback functions? If you have one callback event, then the variables in one function workspace are not going to magically jump into the other workspace, so why do you separate them? For example there is no way for name to jump magically from loaddata_Callback to Read_Callback.
  • You do nothing inside the first for loop anyway, apart from assign one name value to C, which then never gets used. What is the point of that?
  • Calling fopen('C','r') will try to open a file named 'C' in the current directory. If you want to refer to the variable C, then you need to remove those single-quotes so that it is a variable and not a string.
  • Do not use path as a variable name, as path is already defined in MATLAB.
  • You should use fullfile rather than simply concatenating the path and filenames.
  • Why are the uigetfile file-types labeled as being 'All Matlab Files'? Those are not standard MATLAB file extensions.
  • Learn to use the semicolon to suppress displaying outputs.
Something like this should work:
function loadData_Callback(hObject, eventdata, handles)
%
[N_cell,P_str] = uigetfile({'*.erg';'*.e00'},'A better description','Multiselect','on');
%
for k = 1:numel(N_cell)*~isequal(0,P_str)
temp = fullfile(P_str,N_cell{k});
fid = fopen(temp,'r');
zeile = textscan(fid,...)
fclose(fid);
end
cemsi888
cemsi888 on 16 Feb 2015
Edited: cemsi888 on 16 Feb 2015
i have 2 callbackfunctions first button for open files second button read datas. i think if i use your code i will eleminate first button which is open files.
function loadData_Callback(hObject, eventdata, handles)
[name,path]=uigetfile({'*.erg';'*.e00'},'All Matlab Files','Multiselect','on')
c=length([name])
for k=1:c
C=name
end
if isequal(name,0)|| isequal(path,0)
fileName=''
else
fileName=[path name]
end
%B=strcat(name)
set(handles,filePath,'string',fileName)
%--- Executes on button press in Read.
function Read_Callback(hObject, eventdata, handles)
delimiter=';' ;
headerLines=4
formatSpec1 = '%s%s%s%s%s%[^\n\r]'
daten=fopen(C,'r')
zeile = textscan(daten,formatSpec1,181,'delimiter',';','headerLines',4)
fclose(daten);
for i=1:length(zeile{1,1})
tmp=zeile{1,1}{i}
tmp=strrep(tmp,'.','_dot')
zeile{1,1}(i)=cellstr(tmp);
end
for ind=1:size(zeile{1,1},1)
Input_Mappe1.(zeile{1,1}{ind})=magic(size(zeile{1,1}{ind},1))
end
Because I do not want to callculate sometimes whole files in my path!! thats why i divided my calculation first open files then read files!!!
Stephen23
Stephen23 on 16 Feb 2015
Edited: Stephen23 on 16 Feb 2015
The code is currently unreadble: please learn to format your code correctly on this forum. You can use the {} Code button above the textbox, and review it using the preview panel below the textbox.
The code actually does something different to what you described: loadData_Callback does not open any files (or even load them), but merely generates some filepath strings, whereas Read_Callback really does open and read the data files, using fopen and textscan. It should close them too.
i know it and i used it as you see some block are readable and some are not. i do not know why!! but i try to fix it
you can try it now it Looks better!! thanx for help
Are you still having trouble or not? If so, please attach all files necessary for us to run the code with the paper clip icon.
I have still Problem.i added my m file.Thanx Image analyst
could you find any solution or could you give me advice?
Your loaddata callback gets the filename but then it's lost as the function exits. You don't do anything with it. In the other callback you don't use the filename but instead use some undefined variable called C. Why??? Just make the filename global or attach it as a field to the handles structure and use that in fopen().
If you want to keep the filepath generation and file-reading in separate functions, then you will need a way to transfer that filepath information from one callback function to the other. I would suggest using guidata for this. Something a bit like this:
function makeFilename_Callback(hObject, eventdata, handles)
%
[A.path,A.names] = uigetfile({'*.erg';'*.e00'},'description...','Multiselect','on');
guidata(hObject,A);
end
function Read_Callback(hObject, eventdata, handles)
%
A = guidata(hObject);
if isempty(A)||~isstruct(A)
return
end
for k = 1:numel(A.names)
temp = fullfile(A.path,A.names{k});
fid = fopen(temp,'r');
zeile = textscan(fid,...)
fclose(fid);
end
end
I tried global but even though my program does not work :(
Stephen i tried your advice too but it does not work too
my Problem is i can not get Name and path Name as a structurearray or cellarray. first callback function deletes it.and i could not find any Solutions although i read too many pdfs...
cemsi888
cemsi888 on 17 Feb 2015
Edited: cemsi888 on 17 Feb 2015
i got this massage : The function "Read_Callback" was closed with an 'end', but at least one other function definition was not. To avoid confusion when using nested functions, it is illegal to use both conventions in the same file.
Stephen23
Stephen23 on 17 Feb 2015
Edited: Stephen23 on 17 Feb 2015
Some hints to make your own life easier:
  • pay attention to the MATLAB Editor's code warnings. Currently I get a whole pile of orange warnings about small improvements that could be made.
  • format your code consistently. Currently your indenting is quite random: use the standard four space characters for loops and the like. This will make it easier to notice problems like not having enough end's.
  • Learn to use the MATLAB debugging tools. An important part of writing code is knowing how to debug code. Learning to debug makes you much more independent, a better programmer and teaches you new things about the language you are using.
  • Use MATLAB's documentation. Don't just copy the code that someone puts here in an answer. Read about the functions and operations that it uses, and understand how it works.
Attach the fig file also, otherwise we can't run your program.
even now i could not solve the Problem .i used to guidata to Transfer my callback to next step ( or next callback) but i could not success it. Of course i do not want to copy paste codes and l want to learn how it works!!!.
when i used guidata it deletes my structure Array which Name A is and after than i could not use this structure aaray in the next step
Stephen23
Stephen23 on 17 Feb 2015
Edited: Stephen23 on 17 Feb 2015
It seems like GUIDE already uses guidata for saving some handles. Learning to code this without GUIDE is one solution, otherwise use globals.
cemsi888
cemsi888 on 17 Feb 2015
Edited: cemsi888 on 17 Feb 2015
i have already tried it .i think there is insuffucient informations in Internet to learn how global works!!
i think i will ask my questions tomorrow too.it is enough for today...
Just put
global yourVariable;
in whatever function that needs to use yourVariable. Functions that don't have that won't be able to see yourVariable, and functions that do have that will be able to see it.
i want to see some of my Parameters(Input_Mappe1) on Workspace. how can i solve this Problem do you have an idea? is it possible to make it with assigin?
It is possible to do this, but if you only want to view them in order to check if your code is working properly then use the MATLAB debugging tools rather than assignin.
thanx for your help Imagealayst and Srephen Cobeldick i solve this Problem and i can see structure Array on my Workspace which contains my filenames too. the next step is i want to read my datas. but i take this massage from matlab: (without GUI my codes work !!!)
Error using textscan Invalid file identifier. Use fopen to generate a valid file identifier.
Error in gui_versuch>Read_Callback (line 93) zeile = textscan(daten,formatSpec1,181,'delimiter',';','headerLines',4)
Error in gui_mainfcn (line 96) feval(varargin{:});
Error in gui_versuch (line 42) gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)gui_versuch('Read_Callback',hObject,eventdata,guidata(hObject)) here is my code :
if % --- Executes on button press in Load.
function Load_Callback(hObject, eventdata, handles)
filename=uigetfile({'*.erg';'*.e00'},'description...','Multiselect','on')
handles.filename=filename;
guidata(hObject,handles)
assignin('base','handles',handles)
% --- Executes on button press in Read.
function Read_Callback(hObject, eventdata, handles)
filename=handles.filename
if%%%Probe für......11
delimiter=';';
headerLines=4;
formatSpec1 = '%s%s%s%s%s%[^\n\r]';
daten=fopen('filename{1}','r')
zeile = textscan(daten,formatSpec1,181,'delimiter',';','headerLines',4)
fclose(daten);
Could you help me please!!
Stephen23
Stephen23 on 18 Feb 2015
Edited: Stephen23 on 18 Feb 2015
This is probably because your files are located in a different directory to your current working directory, and so if you only supply fopen with the filename it looks in the current directory and cannot find a file with that name.
The solution to this is to also obtain the second output of uigetfile (the filepath), join this onto the filenames (use fullpath to do this, do not use string concatenation), and then pass this full filepath to fopen.
Here is one way to use fullpath with a cell array of filenames:
>> fnm = {'Anna.txt','Bob.txt','Cath.txt'};
>> pth = 'C:\foo\bar';
>> fpt = cellfun(@(s)fullfile(pth,s), fnm, 'UniformOutput',false);
Optionally you could include some handling for the case of if the users deletes the dialog box, or does not select any files.
no all the files are in my current Directory. i opened new m. file to check whether my codes are wrong or not but in this new m file it works properly. here is my code.
if function Load_Callback(hObject, eventdata, handles)
filename=uigetfile({'*.erg';'*.e00'},'description...','Multiselect','on')
handles.filename=filename;
guidata(hObject,handles)
assignin('base','handles',handles)
% --- Executes on button press in Read.
function Read_Callback(hObject, eventdata, handles)
filename=handles.filename
%a=length(filename)
for i=1:length(handles.filename)
temp=handles.filename{1,i}
fid=fopen(temp(i),'r')
delimiter=';';
headerlines=4;
formatSpec1 = '%s%s%s%s%s%[^\n\r]';
zeile = textscan(fid,formatSpec1,181,'delimiter',';','headerLines',4)
fclose(fid);
end
i added some codes too now it works better but i Chose two files but program gave me just one result of file. Could you help about These Problem ?here is my code:
function Load_Callback(hObject, eventdata, handles)
filename=uigetfile({'*.erg';'*.e00'},'description...','Multiselect','on')
handles.filename=filename;
guidata(hObject,handles)
assignin('base','handles',handles)
% --- Executes on button press in Read.
function Read_Callback(hObject, eventdata, handles)
filename=handles.filename
%a=length(filename)
for i=1:length(handles.filename)
temp=handles.filename{1,i}
fid=fopen(temp,'r')
delimiter=';';
headerlines=4;
formatSpec1 = '%s%s%s%s%s%[^\n\r]';
zeile = textscan(fid,formatSpec1,181,'delimiter',';','headerLines',4)
assignin('base','zeile',zeile)
assignin('base','temp',temp)
fclose(fid);
end
Stephen23
Stephen23 on 23 Feb 2015
Edited: Stephen23 on 23 Feb 2015
You should probably end your functions. Defining functions without end can be a bit ambiguous where they end.
Every time that you call
assignin('base','zeile',zeile)
it replaces any data that already exists in your workspace with the name zeile with the some new data with the same name. If you want to see all of the data, then you need to change the name each time.
Actually I would recommend that you avoid using assignin completely, and learn to use the debugger to check variables.
Note that creating lots of variables with dynamic names is a really bad idea. In case you are interested, here are some pages explaining why dynamically assigning variable names is a really bad idea in MATLAB:
cemsi888
cemsi888 on 23 Feb 2015
Edited: cemsi888 on 23 Feb 2015
if i did not use assigin i can not see zeile on Workspace!! the Problem is i can not use properly for Loop in textscan. iused Debugger too. for example i Chose 3 files but matlab Show just last file on Workspace.i could not find any solution
Stephen23
Stephen23 on 23 Feb 2015
Edited: Stephen23 on 23 Feb 2015
"if i did not use assigin i can not see zeile on Workspace": correct, and this is exactly the point. By choosing to use assignin you are making your own life more difficult, as doing this requires dynamic names and writes over existing variables. Bad idea. You should not be using assignin to move variables around between workspaces. Consider instead:
  • Use the debugging tools to view a function workspace.
  • Use output arguments to return values from a function to the base workspace.
Each function (and base) has its own workspace. What happens inside a function should be invisible to the outside world: the algorithm is irrelevant, the only thing that matters is the output argument. The only time it should be necessary to look at a function's workspace is when it is being written or debugged, in which case the debugger is the best tool for this job. If you need to move data from a function workspace to another workspace, use the output arguments of the function itself. Learn to avoid assignin, and your code will become better, neater and more reliable.
today i have couple of questions too. if i choose 1 file it gives me charachter (filename) thats why i can not run my program but when i choose 3 files i can run program and i can see filename as a cell Array
function Load_Callback(hObject, eventdata, handles)
filename=uigetfile({'*.e00';'*.erg'},'Pick a file','Multiselect','on')
handles.filename=filename;
guidata(hObject,handles)
assignin('base','handles',handles)
second question .i want to copy all paramaters in mappe_(i) thats why today i wrote new codes too .it creates Mappe_ structure Array but the Problem is all Mappe_ contains just the Parameters of last file that i Chose before . how can i solve this Problem ?
function Load_Callback(hObject, eventdata, handles)
filename=uigetfile({'*.e00';'*.erg'},'Pick a file','Multiselect','on')
handles.filename=filename;
guidata(hObject,handles)
assignin('base','handles',handles)
% --- Executes on button press in Read. function Read_Callback(hObject, eventdata, handles)
filename=handles.filename
for i=1:length(handles.filename)
temp=handles.filename{1,i}
fid=fopen(temp,'r')
delimiter=';'
headerlines=4;
formatSpec1 = '%s%s%s%s%s%[^\n\r]';
zeile= textscan(fid,formatSpec1,181,'delimiter',';','headerLines',4)
meinmappe = cell(length(handles.filename),1);
for j=1:length(handles.filename)
meinmappe{j} = sprintf('Mappe_%u',j)
end
for j=1:length(handles.filename)
assignin('base',meinmappe{j},zeile);
end
assignin('base','zeile',zeile)
assignin('base','temp',temp)
fclose(fid);
end
for first question why occurs cell when i choose 2 files and why does not occur cell when i choose 1 file.
It needs to send back a cell array when there's a list of strings because all the strings may not be the same size. If there's just one I'd guess it's a string because that's a lot simpler and no cell array is required.

Sign in to comment.

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!