how can i open my files?
1 次查看(过去 30 天)
显示 更早的评论
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);
采纳的回答
Image Analyst
2015-2-13
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 个评论
Stephen23
2015-2-13
Then use the dir option, as Image Analyst suggested and as the Wiki also explains.
cemsi888
2015-2-16
编辑:cemsi888
2015-2-16
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
2015-2-16
编辑:Stephen23
2015-2-16
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
2015-2-16
编辑:cemsi888
2015-2-16
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
2015-2-16
编辑:Stephen23
2015-2-16
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.
cemsi888
2015-2-16
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
Image Analyst
2015-2-16
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
2015-2-16
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
2015-2-16
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
2015-2-17
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...
Stephen23
2015-2-17
编辑:Stephen23
2015-2-17
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.
cemsi888
2015-2-17
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!!!.
cemsi888
2015-2-17
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
Image Analyst
2015-2-17
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
2015-2-18
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?
Stephen23
2015-2-18
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.
cemsi888
2015-2-18
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
2015-2-18
编辑:Stephen23
2015-2-18
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
2015-2-23
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
2015-2-23
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
2015-2-23
编辑:Stephen23
2015-2-23
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:
Stephen23
2015-2-23
编辑:Stephen23
2015-2-23
"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
2015-2-24
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
2015-2-24
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
cemsi888
2015-2-24
for first question why occurs cell when i choose 2 files and why does not occur cell when i choose 1 file.
Image Analyst
2015-2-24
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Low-Level File I/O 的更多信息
标签
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)