I want to save a matrix as .text with a variable filename. Currently I'm saving my file using the function dlmwrite(name,matrix); This is only working with a pre-set filename. Is there a way to make the name of the file variable?
A window that pops up that ask for a filename to write to just as 'Uigetfile' does with opening a file would be ideal. Does anyone know if Matlab got a function just like that for writing text files?
You can use uiputfile to graphically get the file name. For example:
[filename, pathname, filterindex] = uiputfile('', 'Select file');
Then use dlmwrite to save a variable, say data, to that file:
dlmwrite(fullfile(pathname, filename), data)
Related
I want the user of a script I'm writing to be able to navigate to a file containing their data and to load the data into the workspace. For example, if a csv file contains two cells with the values 1 and 2 respectively, I want the user to simply choose this file and those two values will be assigned to a variable in the workspace.
I have looked at using:
filename = uigetfile('*.xlsx','*.csv')
But that just returns the name of the file. Perhaps I could construct a full path to where the file they choose is found, and then read it in that way (using xlsread or csvread) but I think I'm probably missing something here. It seems that there should be a more straightforward way of doing it.
I believe that you're looking for the uiopen() function. This function will:
Open dialog box for selecting files to load into workspace.
On default, this function will display in a file explorer dialog box with the filter set to all MATLABĀ® files (with file extensions *.m, *.mlx, *.mat, *.fig, *.mdl, and *.slx).
However, you can import data from data files like CSV files and spreadsheets as well. Simply select the (All Files) option for the Files of Type field.
Once you've selected the data file you're interested in, you will be prompted with another GUI object that previews the data you are about to load into MATLAB's workspace. If you're satisfied with the format of the variables presented in the preview, simply hit the green check-mark at the right-side of the tool-box ribbon in the GUI object and, huzzah, all of the data file's contents have been loaded into separate variables (named according to their respective headers).
Alternatively, though this is undeniably a longer-winded and uglier approach, if you'd like to use the filename returned from uigetfile('*.xlsx', '*.csv'), you could use the importdata() function. This will output a struct that contains each of the variables from your data file as a separate field:
[filename, pathname] = uigetfile( ...
{'*.csv;', 'CSV file (*.csv)';
'*.xlsx', 'Excel Spreadsheet file (*.xlsx)'; ...
'*.*', 'All Files (*.*)'}, 'Pick a File to Import');
full_filename = fullfile(pathname, filename);
[A, delimiterOut] = importdata(full_filename);
I have a short question regarding the copyfile function within MATLAB. Basically I want to copy a file from a different user selected directory/file to the current directory (where the function is run from). I am struggling with how to do this.
So far I have:
[jxlfilename,jxlfilepath] = uigetfile({'*.jxl'}, 'Pick a File');
copyfile(????)
I have read the help that MATLAB offers, but I just can't seem to figure it out.
The syntax for copyfile is
copyfile(source,destination);
The function to concatenate paths and filenames is fullfile .
The current directory is selected with .
Together this gives you
[jxlfilename,jxlfilepath] = uigetfile({'*.jxl'}, 'Pick a File');
copyfile(fullfile(jxlfilepath,jxlfilename),'.');
you were almost there, once your selected your file do:
copyfile(strcat(jxlfilepath,jxlfilename))
and if you don't specify a second argument copyfile will copy the file to the current folder and the strcat(jxlfilepath,jxlfilename) will construct the string with path and filename.
Or
copyfile(strcat(jxlfilepath,jxlfilename),'newname.jxl')
if you want to assign a new name to the file.
I want to write the file name in the gui edit text and save edit text as my filename. Than I could be able to save filename as I wish. For instance; My file is an Neural network file which could be save as *.mat file; here is my code
%network_name is my edit text
name = get(handles.network_name,'string');
name = net;
save name
But it doesn't work I can't manage file name from edit text :(
It saves as name that l wrote next to save (name.mat). Thanks for your any answer...
Why name = net;?
That aside, if you want to pass the filename as a variable to the save function, you need to use this syntax instead:
save(name)
save name is the "command form", and save('name') is the corresponding "function form". As you can see you can only give string inputs when using command form, whilst you actually want to pass a variable.
To conclude: if you are passing variables to a function, use function form.
If you want to save particular variables, use:
save(name,'net');
Note that name (which we want to be the string contained in the variable) isn't in quotes and net (the name of the variable we want to save) is.
A warning about this is actually buried in the documentation for save.
Do not use command form when any of the inputs, such as filename,
are variables.
save name
will save all of the variables in your workspace to a *.mat file called 'name'
Also your code is basically overwriting itself, line 2 sets the variable name to be a string, but then line 3 writes some data net to that variable.
I'm a little confused as to exactly what you want but I think you want to save the variable net to a mat file with the name that you read in the string from get(handles.network_name,'string')
If that is what you want to do then its just
save(get(handles.network_name,'string'), net)
If you want to save all the open variabile in the workspace then its just
save(get(handles.network_name,'string'))
I'm having troubles opening a file with different fid that is previously opened with 'Input'.
Example:
fid1=fopen((input('Video: ')),'r');
fid2=fopen((input('Save as: ')),'w');
...
...
fid3=fopen('','r'); % Here I want to open the input video same as fid1
I don't know how to request returned value, in this case the name entered.
Entering fid3=fid1 after declaration of fid1 does not help me.
I don't want to ask for the name to be input again (I.e fid3=fopen((input('Video: ')),'r');).
To do what you want to do, you need to capture and store the filename somewhere. I really don't think that information can be recovered from a file pointer.
I am very new to Matlab so sorry if this is trivial. I have a matlab code done by another student and I am trying to do something. There is a pattern generated which should be saved to a .png image.
For now, it asks for user input on where to save the file like following:
[filename,pathname,dummy] = uiputfile('*.png');
imwrite(image_blobs,[pathname filename '.png'],'png');
I need it to be saved as soon as the pattern is generated, I did try to do the following:
pathname='H:\matlab_modified';
filename='pic';
imwrite(image_blobs,[pathname filename '.png'],'png');
but this will not work.
I also did try the save but the save will not save it as image, right ?
Any idea how to do it ?
thanks
pathname='H:\matlab_modified';
filename='pic';
% build full filename from path, filename and extension
full_filename = fullfile(pathname, filename, '.png');
imwrite(image_blobs, full_filename, 'png');