Opening files from uimenu in matlab - matlab

i am still working at my project and i again i hit a wall.
In this case i am trying to open a .pdf file from uimenu, i have searched through functions, i think i found the right one:
x = 'D:\MATLAB\Author.pdf';
y = 'D:\MATLAB\Bibliography.pdf';
f=uimenu('Label','ProjectData');
uimenu(f,'Label','Author','Callback','fopen(x)');
uimenu(f,'Label','Bibliography','Callback','fopen(y)');
uimenu(f,'Label','Close','Callback','close',...
'Separator','on','Accelerator','Q');
But the problem is that when i click on author or bibliography nothing happens, no errors, nothing.
The only thing that appears in command window is this :
ans =
-1
The .pdf files are in the same folder as the rest of the .m files.
Please help, Thank you !

That's because MATLAB can't open your file.
From the fopen documentation (available here) (the bold font is from me):
fileID = fopen(filename) opens the file, filename, for binary read
access, and returns an integer file identifier equal to or greater
than 3. MATLABĀ® reserves file identifiers 0, 1, and 2 for standard
input, standard output (the screen), and standard error, respectively.
If fopen cannot open the file, then fileID is -1.
So MATLAB is giving this fileID of -1. If you want MATLAB to open the appropriate application for your pdf file (i.e. Adobe Acrobat), you can use open.
If you want to actually open pdf as images in MATLAB, there is probably some function on the File Exchange.
Hope that helps!

Related

How to add unique image formats to Matlab's imread, and Matlab's drag and drop?

We have a number of internal image formats which I process in Matlab. I have read/write functions for all of them. For specificity, consider the TGA image format, for which there is a file exchange image reader.
Matlab has reasonable drag and drop support for image formats supported by imread.
That is, you can drag an image from explorer, drop it on the "Workspace" pane, and Matlab will read in the image, and copy it into your workspace.
I'd like to be able to add drag and drop support, and imread support for TGA files. (imread has nice autocomplete for filenames for instance, tga_read_image does not.)
I think this is what you are looking for. Quoting the official documentation:
open name opens the specified file or variable in the appropriate
application
You can extend the functionality of open by defining your own
file-handling function of the form openxxx, where xxx is a file
extension. For example, if you create a function openlog, then the
open function calls openlog to process any files with the .log
extension. The open function returns any single output defined by your
function.
For example:
function opentga(file)
% Your logic for reading and, eventually,
% displaying TGA files when drag and drop
% or other opening events occur.
end
And here is a full working example directly taken from the link:
function opentxt(filename)
[~, name, ext] = fileparts(filename);
fprintf('You have requested file: %s\n', [name ext]);
if exist(filename, 'file') == 2
fprintf('Opening in MATLAB Editor: %s\n', [name ext]);
edit(filename);
else
wh = which(filename);
if ~isempty(wh)
fprintf('Opening in MATLAB Editor: %s\n', wh);
edit(wh);
else
warning('MATLAB:fileNotFound', ...
'File was not found: %s', [name ext]);
end
end
end
An alternative path consists in overloading the uiopen function, as shown in this File Exchange release.
Starting off from Tommaso's answer, I created the following M-file on my MATLAB path:
function out = openics(filename)
img = readim(filename);
if nargout==1
out = img;
else
[~,varname] = fileparts(filename);
disp(['assigning into base: ',varname])
assignin('base',varname,img);
end
Dragging and dropping an ICS file onto MATLAB's command window shows the following on the command line:
>> uiopen('/Users/cris/newdip/examples/cermet.ics',1)
assigning into base: cermet
Check:
>> whos cermet
Name Size Bytes Class Attributes
cermet 256x256 65714 dip_image
Reading the code for uiopen (you can just type edit uiopen) shows that this calls open with the filename, which then calls openics with the filename and no output argument.
You can also type
img = open('/Users/cris/newdip/examples/cermet.ics');
to call openics and load the image into variable img.
NOTE 1: I'm using ICS because I don't have any TGA images to test with. ICS is a microscopy image file format.
NOTE 2: readim is a function in DIPimage
NOTE 3: This is cool, I had never bothered trying to drag and drop files onto MATLAB before. :)
The other answers address the "drag and drop" question. They do not address the question of how to integrate a proprietary image format into imread. This can be done fairly straight forwardly with the imformats command.
The issue of how/why it took me 3.5 years to figure that out will remain unanswered I'm afraid.... The feature has been around for 15+ years.

How to open a .p file with fopen in Matlab

I have an third-party file with the extension .p and I try if it is possible to open it using the function fopen:
fopen(title.p)
This returns 3, which is a file identifier according to the help function:
help fopen
fopen - Open file, or obtain information about open files
This MATLAB function opens the file, filename, for binary read access, and returns an integer file identifier equal to or greater than 3.
What should I do to see (if possible) the content of the file?
A file with the extension .p in the context of matlab probably refers to a pcode file. It is obfuscated bytecode, you won't be able to read anything useful. Nevertheless you can run the function calling it like any other matlab function.
I was able to open the file using
fid = fopen('file.p')
fgetl(fid)
However, the content was not useful, indeed.

Opening a textfile in MatLab

I have written a simple textfile using notepad, and I placed the text file in the same folder as a matlab script I want to run. I pretty much want to make a script so that when the user clicks run a pop-up of my text file will come up purely within matlab.(I will later include separate figures, graphs to pop up at the same time.)
I tried researching, and I found fopen. But when I do fopen('FileName.txt','r'); nothing happens?
I am not sure of what you are looking for, but I guessed that you need to open a file and read its content using Matlab.
fid = fopen('FileName.txt','r'); % generate a file identifier
if fid < 0
error 'Unable to open file ... check file location'
else
while ~feof(fid) % while there is a line to read
disp(fgets(fid))
end
end
fclose(fid);

fprintf after opening file using fopen does not work in matlab

May be my tired eyes are ignoring something trivial. But I am not able to get the following code work in matlab. I need to open an existing file and write text to it.
filename='E:\\data_bak\\test.txt';
fileId=fopen(filename,'r+');
if(fileId~=-1)
myline=fgetl(fileId);
nbytes=fprintf(fileId,'%s\n','testdata')
fclose(fileId);
end
I am using matlab 7.9.0 on windows 7. I have tried rt+ for permissions in fopen. Also, fileId is not equal to zero as I am able to read the line in the variable myline. In addition, fprintf('%s\n','testdata') successfully prints testdata on the matlab prompt.
The value assigned to nbytes is 9. So it does appear it has written to file but when I open the file in text editor, I am not able to find text 'testdata'.

Recover a longer list of recent files from the Matlab editor

I have lost the location of a .m file I recently wrote with the Matlab editor. I don't remember how I named it so the usual finder search is not helping.
The Matlab editor lets me open 'recent files' but only the few newest. Is there a way to recover a longer list of recently opened files?
That information seems to be stored in the Matlab preference folder. That folder is given by the function prefdir.
Specifically, the file 'matlab.prf' seems to contain the list of recent files. To open that file and inspect it manually you can use
open(fullfile(prefdir, 'matlab.prf'))
Recent file information seems to be contained in lines beginning with EditorMRU. I have observed that in R2010b and R2014b. Other Matlab versions may behave differently.
You could also programmatically read that file with importdata,
x = importdata(fullfile(prefdir, 'matlab.prf')); %// R2010b or R2014b
x = x.textdata; %// include this line if using R2014b; not if using 2010b
This gives x as a cell array of strings, where each string is a line of that file. Then look for lines containing the substring 'EditorMRU':
y = x(~cellfun(#isempty, strfind(x, 'EditorMRU')));
I don't know how many recent file names are stored, though.