save vector as Matlab file - matlab

Does the save function in Matlab save the thing saved in the same project file?
I'm trying to save a vector as 'mat' file.
This is my code :
function facePts = getFacePts(faceFileName)
if(exist('faceFileName','file')==2)
facePts=load('faceFilename.mat');
return;
end
img=imread(faceFileName,'tif');
showImage(img);
[x,y]=ginput(3);
facePts=[x,y]';
facePts=facePts(:);
save faceFileName.m, facePts; %%%%% HERE
end
The function compiles but I can't find the file I saved

I guess you want to do this:
save('faceFileName.mat', 'facePts');

Ok, so I figuered out that the path of the current folder wasn't the path of my Project.
I changed that by going to 'Desktop' in the Bar, checked 'Current Folder' and chaged the path there.
Now it works !

Related

Deploying Matlab gui exe with Weka Models

My problem is that when i deploy my Program along with weka models. The resulting error is that it cant read the weka model file.
Here is the structure of the Files
ModelLoadmodel.m
Contains The ff code
javaaddpath('weka.jar');
addpath('SurfModels');
loadedModel = wekaLoadModel('RandomForestK40Surf.model');
The Folder wherein all of these file contains has a structure of
WekaLoadmodel.m
SurfRandomForestK40.model
Folder"WekaLab" which has a
WekaLoadModel.m
Weka.jar
When i try to use the commandline function of
loadedModel = wekaLoadModel('RandomForestK40Surf.model');
The loadedModel is successfully loaded but when i deploy it using deploytool and the output is Error reading model file
The WekaLoadmodel.m Contains
if ~exist(filename, 'file')
error('WEKALAB:wekaLoadModel:FileNotFound', 'No file found at %s', filename);
end
%% Code
try
modelObj = weka.core.SerializationHelper.read(filename);
catch err
error('WEKALAB:wekaLoadModel:ReadError', 'Error reading model file at %s', filename);
end
end
It came from https://www.mathworks.com/matlabcentral/fileexchange/58675-wekalab--bridging-weka-and-matlab . Is there some sort of problem when loading other file extension in matlab deploytool?
The answer is don't put it in a folder. Just take all the .m files and compile it. Simple as that

Matfile isn't loading the structure's field names

Background
I encountered some strange behaviour with the function "matfile" in Matlab 2016b - not sure what's going on, and I can't replicate it or create a test case.
I have a structure, which I saved to a server, like so:
PathToFile='ServerPath\My Documents\MyProj\testMatF.mat';
save(PathToFile,'-struct','myStruct'); %I tried the -v7.3 flag
Problem
Then I read it in with:
m1=matfile(PathToFile);
On other, very similar structs, I can do:
myFields=fieldnames(m1);
But for this one file I can't, all I get is the auto "Properties" field.
What I've tried
myFields=who(m1) - gives me list of fieldnames... sometimes. I don't know the who function well, but it seems, if I intersperse who m1 then myFields=who(m1) it works.
Explicitly typing m1.TheFieldName, works.
Moving the file to a location on the comp, like C:\Data\. Then using fieldnames works.
Using load, works.
Does it have to do with the server access, corrupted file, matfile properties? One other weird thing is some of my .m files in this particular folder, when I try to open them results in: Does not exist, when clearly it does, since I click on it and can use the run function with it... Additional: Windows 7. Recently updated license.
Please let me know what info you can use to help out. Either to create a new file that will work, or fix the problem with the current file. Thanks.
EDIT
Example output in my command window - seemingly incomprehensible...
m1=matfile(fullfile(projPath,'NexusMarkersProcessed.mat'),'Writable',false)
m1 =
matlab.io.MatFile
Properties:
Properties.Source: '\bontempi.medicine.utorad.utoronto.ca\home\PT\zabjeklab3\My
Documents\Data\Active Projects\JC_Hip\FL1502\FL1502\Patient
Classification 2\NexusMarkersProcessed.mat'
Properties.Writable: false
Methods
K>> m1.Properties.Source
ans =
\bontempi.medicine.utorad.utoronto.ca\home\PT\zabjeklab3\My
Documents\Data\Active Projects\JC_Hip\FL1502\FL1502\Patient
Classification 2\NexusMarkersProcessed.mat
K>> java.io.File(m1.Properties.Source).exists()
ans =
logical
0
Pause to paste in this window... go back:
java.io.File(m1.Properties.Source).exists()
ans =
logical
1
K>> who(m1)
Your variables are:
Patient10 Patient5 Patient9 Patient11 Patient6 Patient3
Patient7
K>> who(m1) K>> who(m1) K>>
java.io.File(m1.Properties.Source).exists()
ans =
logical
0
K>>
So it sometimes finds the file, and can read it in. Othertimes it cannot - is this to do with the fact that it's on a network drive?
Any help is appreciated.
This issue is caused by accessing a file on a network drive. One workaround is to copy the file to a local drive (C: is used), and then use matfile, use the result as needed, then replace the network drive file, and delete the local file, to return things to their original state. Some research made me realize things are slower than they need to be if any files, even the .m ones, are on the network. Here's a function that worked:
function [m,noData]=safeMatFile(FilePath,safeFilePath)
%FilePath: absolute path to where the file is on the network
%safeFilePath: absolute path to where the file will be temporarily copied locally, C://
%safeDir='C:\Data';
%safeFold='tempFolder12345679';
%[~,tempDir,~]=mkdir(safeDir,safeFold);
%safeFilePath=fullfile(safeDir,safeFold,FileName);
noData=0;
dirFile=dir(FilePath);
if (length(dirFile) == 1) && ~(dirFile.isdir)%OR java below OR exist(forceFilePath,'file')==2
%if there is a file, make a temp folder on the C drive
if ~(java.io.File(safeFilePath).exists() && java.io.File(safeFilePath).isFile()) %ensure file doesn't exist, check dir too: || isempty(tempFolder)
copyfile(FilePath, safeFilePath);%moves existing file to backup folder
else
warning('SKIPPING (%s) - an old file of the same name was there, delete it!\n',safeFilePath);
return
end
%Load the temp local file into matlab
m=matfile(safeFilePath,'Writable',true);
else
m=[];
noData=1;
end
end
Then do stuff with m... and at the end:
function overwriteOldFiles()
if (java.io.File(safeFilePath).exists() && java.io.File(safeFilePath).isFile())
java.io.File(FilePath).delete();
java.io.File(safeFilePath).renameTo(java.io.File(FilePath));
end
end
and
function deleteTempFiles()
if (java.io.File(safeFilePath).exists() && java.io.File(safeFilePath).isFile())
java.io.File(safeFilePath).delete();
end
end
... Then rmdir if necessary.
Note, I tried different ways of checking if the file exists (I think the first is fastest and most reliable):
dirFile=dir(FilePath); if (length(dirFile) == 1) && ~(dirFile.isdir)
if (java.io.File(FilePath).exists() && java.io.File(FilePath).isFile())
if exist(FilePath,'file')==2

How to add input parameters for matlab application execution

I'm writing a script in MatLab and I need to execute a program (written in C) as one of the lines (it generates an output file).
My current code is:
!collect2.exe infile.csv <-- I want to be able to change this to a variable but I can't
My question is, is there a way for me to either:
A. put a variable in place of infile.csv such as
!collect2.exe filedir
or
B. run multiple files without a variable
Thanks in advance :)
Edit:
filedir = input('What is the directory with quotes?');
!cd /
cmdString = ['cd ', filedir];
system(cmdString);
Edit #2:
Never mind, I fixed the issue. Thanks for all of your help!
Use the system function.
Example:
filename = 'infile.csv';
cmdString = ['collect2.exe ', filename];
system(cmdString);
you can create a variable called filename, per your example it would be
filename='infile.csv';
or alternately have you tried using this to launch multiple files at once?
!collect.exe {infile.csv,infile1.csv,infile2.csv};

Accessing image from a specific folder in matlab

Can someone please explain to me what the code below isnt working?
myFolderdepth = 'C:\Users\owner\Desktop'; %Specify Directory to get image from
Depth = dir (fullfile(myFolderdepth,'shower_depth','*.png'))%%Get images from file named shower_depth
Depth_name = {Depth.name}'; %gets the name
figure;
imshow(Depth_name{3})
The error message I get is as follows:
Error using getImageFromFile (line 11)
Cannot find the specified file:
"Depth_003.png".
The directory I am working in is: C:\Users\owner\Desktop
The name of the pictures are Depth_001,Depth_002,Depth_003,......
Oddly enough, I have another folder that has images and if I change the 'shower_depth' to the other folder name, it works fine.
Thank you!
P.S I did some further experimentation, it turns out its because of the way the image is named; if its Depth_01.png thats fine it works but Depth_001.png is not okay
Anyone knows why?
The following command:
Depth = dir (fullfile(myFolderdepth,'shower_depth','*.png'))
only gets the relative names of the files. This means that the file names are only retrieved, not the full path to the file. Take a look at the error that you're getting:
Error using getImageFromFile (line 11)
Cannot find the specified file: "Depth_003.png".
Do you see the path of where your images in the above file name? Nope! You only see the file stored in the directory. You need to specify the full path of where the image is located.
What you need to do is append the directory as well as the image itself as the string you supply to imshow:
myFolderdepth = 'C:\Users\owner\Desktop'; %Specify Directory to get image from
Depth = dir (fullfile(myFolderdepth,'shower_depth','*.png'))%%Get images from file named shower_depth
Depth_name = {Depth.name}'; %gets the name
figure;
imshow(fullfile(myFolderDepth, Depth_name{3})); %// CHANGE HERE
Depth_name is only the image name. You have to read the image before show it. The modified code is below:
im = imread(Depth_name{3});
imshow(Depth_name{3});

How can I change the name of the file being saved without editing the code? MatLab

I am working on an experiment that take a lot of data samples and save it to different video files. For example, I take data and save it to a file called "slowmotion.avi" and then I take another set of data and save it to another file called "normalspeed.avi".
I am trying to find a way that I could change the name of file being saved without editing the code. The way I am using right now makes me have to open the code and change the name of the file directory within the code and then save the code. I want to make it a lot faster and easier to use.
I have tried the following code to fix this problem but it doesn't work.
graph=input('Graph of experiment: ');
vidObj = VideoWriter('%3.1f.avi\n',graph);
.....
Hope I didn't confuse you.
A possible solution:
graph=input('Graph of experiment: ','s');
vidObj = VideoWriter([graph,'.avi']);
The 's' in the input() function indicates that the expected input is a string, and [graph,'.avi'] simply concatenates both strings.