You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
how can i open my files?
1 view (last 30 days)
Show older comments
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
on 13 Feb 2015
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
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
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
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
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.
Image Analyst
on 16 Feb 2015
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.
Image Analyst
on 16 Feb 2015
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().
Stephen23
on 16 Feb 2015
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
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
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.
Image Analyst
on 17 Feb 2015
Attach the fig file also, otherwise we can't run your program.
cemsi888
on 17 Feb 2015
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!!!.
Image Analyst
on 17 Feb 2015
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.
cemsi888
on 18 Feb 2015
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
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.
cemsi888
on 23 Feb 2015
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
cemsi888
on 23 Feb 2015
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
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
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
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.
cemsi888
on 24 Feb 2015
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)
cemsi888
on 24 Feb 2015
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
Image Analyst
on 24 Feb 2015
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.
More Answers (0)
See Also
Categories
Find more on Entering Commands in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)