how do you program the file path when you do not know where the person will save the matlab file you send them?

I have a program that reads in a graphic .tiff file that I saved in the same folder as my matlab program. I have the file path set to the saved folder. When I email my matlab program and graphic file to someone to use, how do I get program to work when I do not know where they will save the files and so do not know the future file path? Here is code section:
%this is where user is shown the diagram picture
fprintf('Look at figure 1 to see your input choices: \n \n');
%read in the picture of the beam and inputs
beamPic = imread('C:\mymatlab\beam.tiff');
%the file path above needs to be changed depending where you save files
%show the beam pictures
imshow(beamPic);

6 Comments

I don't understand the question. Maybe dir is what you are looking for
this question is somewhat ill-posed. what if they have multiple files named 'beam.tiff' on their filesystem? any method that works would return multiple paths, and you'd have to have a way to pick one.
I will rephrase. I have a graphic file named "beam.tiff" and I need it to allow my program to work. How do I send my program and send the graphic file to a user so they may run my program so that they can hit run and use my program to do the engineering that it is designed to do without them entering a file path? why cant I just imbed the graphic in the program?
jessupj that is my question. I do not know what is in "their" filesystem. How do I get someone like you for instance to use my program by only hitting start program? Like if I handed them a floppy disc and all they ahd to do is hit start. How do I do the equivilent with matlab? In my code it says where the files are on MY computer so it owuld not work on their computer. How to fix this problem so they don't have to do anything to run my program?
Make sure to share the tiff in the folder as your function. That way it will work the same as a floppy disk.
I already answered, https://www.mathworks.com/matlabcentral/answers/525797-how-do-you-program-the-file-path-when-you-do-not-know-where-the-person-will-save-the-matlab-file-you#answer_432769

Sign in to comment.

Answers (3)

If you don't want to write code to search multiple directories to find the files, your code is going to have to ask the user to input the directory.

2 Comments

thank you that is what i was afraid of
How do I make matlab search mulyiple directories?
Why can't matlab piggy back a file that it needs to work just like if we were talking about a function. Like volume but in cubic yards I could have a function that the main program neeeds to work properly. I think I am missing something here or this sharing of programs would be worthless. I want my files to all be saved in their own directory together in one click. In java I can make all my dependant programs cohabitate. Why can I not have matlab treat them the same way? If I have a function my program needs, why should it ot be required to be attached?
If you send the files together it is up to the user to not separate them. So I don't really understand what feature you are asking for.
You can retrieve the path of function and see if the tiff file is in that folder. Would that solve it?

Sign in to comment.

DIRS = dir('**/beam.tiff'); % gets a structured list of matching files
% ...
% additional criterion here to choose which to pick.
k=1; % assume you want the first match
beamPic = imread( [DIRS(k).folder '/beam.tiff']) % maybe
imshow(beamPic);
If sending the source code is okay, then bundle an app
You would use mfilename('fullpath') to find out the name of the .m file, and could then extract the directory part with fileparts()
If you are building a .exe then tell the application compiler to add the file to the executable, and then in the program open it without providing a path. The file will be unbundled into a temporary directory whose name you can get with ctfroot() .

Categories

Products

Asked:

on 15 May 2020

Commented:

on 19 May 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!