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');
Related
I've been trying to make the output shown in the text file instead of the command window. Im blur right now as i already look at a lot of example but it always show the error on fprintf. I try to edit the code of fprintf(fid,'%s\n',word);%Write 'word' in text file (upper) in one of the Matlab example which is Automatically Detect and Recognize Text in Natural Images.
This is the link of the code.
https://www.mathworks.com/help/vision/examples/automatically-detect-and-recognize-text-in-natural-images.html?s_tid=srchtitle
Basically the above link display the output on the command window. But, i need it to be on the txt file.
Im really new to this, i want to know what code do i need to put, how and where should i put the fprintf to make the output shown on the text file and not on the command window.
Also, can i save the text file after that? do i to put any additional code?
I really need your help. Thank u in advance!
It seems you're looking for the fopen() method. It takes two parameters, the first being the name of the file you'd like to write to, and the second being the mode. If the file specified does not exist in the root directory, it will be created on execution.
fileID = fopen('exp.txt','w');
fprintf(fileID, fid,'%s\n', word);
fclose(fileID); % Make sure to always close the stream after finishing
More on fopen() here
I want to save a matrix (e.g. "PTX_Data_Raw.mat") in another folder (e.g. Temp folder). I have written below code:
mkdir('D:\Projects\ProgrammingPart\Method2_FinalApproved\8-congruent','Temp');
filename=('D:\Projects\ProgrammingPart\Method2_FinalApproved\8-congruent\Temp');
save(filename,'PTX_Data_Raw.mat');
but it didn't work. Does anybody can help me for solving this problem?
THX
Going with your comments, you are using save wrong. The first parameter is the filename you want to call the MAT file and second parameter and onwards are the variables you want to save.
Therefore, you need to make sure filename contains the entire filename, including the path followed by the actual name of the MAT file you want. After, the second parameter is PTX_Data - the name of the matrix you want to save.
mkdir('D:\Projects\ProgrammingPart\Method2_FinalApproved\8-congruent','Temp');
%// Change
filename=('D:\Projects\ProgrammingPart\Method2_FinalApproved\8-congruent\Temp\PTX_Data_Raw.mat');
save(filename,'PTX_Data'); %// Change
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 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)
I am trying to manipulate and save an image to file and it doesn't seem to be working from a function. It does, however, work in the command window. I have tried save, saveas, fprint, and others with no luck.
A = imread('contourSS.jpg'); B = rgb2gray(A);
imwrite(B, 'new_image.gif', 'gif');
Nothing shows up in the MATLAB directory when I run this code from a function, but it does show up in the MATLAB directory when I run it from the command window. Any ideas?
Thanks in advance.
Are you sure the you're saving the file to the correct directory? Try adding adding disp(pwd) to the function, it will display the directory that you are saving too.
Also its generally a good idea to use complete paths when saving a file. Consider changing your code to this:
imgDir = /home/user/image;
readfile = fullfile( imgDir, 'contourSS.jpg');
writefile = fullfile( imgDir, 'new_image.gif');
A = imread(readfild); B = rgb2gray(A);
imwrite(B, writefile, 'gif');