How do I run a function I downloaded?

So I downloaded this script nsumk to do some cool partition of integers for me: https://www.mathworks.com/matlabcentral/fileexchange/28340-nsumk
When i open it in matlab it appears in the editor window, I think it's like in the place where I would normally write scripts (once I get that advanced, haha!). Anyway, what do I have to do from here to get this functiln nsumk running in my command line?? I've tried typing in numk and stuff like that but can't get it to work.
If you can't tell, i'm a total scrub at this stuff, but i'm very interested!!
Simple explanations appreciated!! Peace out!!!!

Answers (1)

Mario Malic
Mario Malic on 18 Sep 2020
Edited: Mario Malic on 18 Sep 2020
Please see John's comment as it's important.
Place the function in your current folder and call it from command window, or in write it in script.
[m,x] = nsumk(n,k)

8 Comments

NO. Don't move the function around, putting it in your CURRENT folder. This is bad avice!
Instead, learn about the search path. You might, for example, have one directory wher you put ALL douwnloaded functions. Put that directory on your search path. And don't forget to save your search path.
In the case of complete toolboxes, you put them in their own directories. also on the search path.
help addpath
help pathtool
help savepath
Why don't you want to move functions around?
Because if you do, then every time you move to a different directory, you need to move all of those functions around, into the new directory.
Once you have your new function in their own directories on the search path, you can now use them from any location, as MATLAB will always know they exist.
Finally, when you upgrade to a new MATLAB release, you can use my export_search_path utility, found on the file echange for download. It allows you to directly export the current search path to a file on your drive. Then open the new MATLAB release, and use the function import_search_path. It will recreate the entire search path from your previous release, but in the new release.
Learn about the search path. Your coding experience will be greatly improved for doing so.
Mario Malic
Mario Malic on 18 Sep 2020
Edited: Mario Malic on 18 Sep 2020
I agree with everything what you said.
What I ment by current folder is that, user who writes code, one would create a folder, let's say on 'Desktop\MATLAB_Work\Project1\' and place his script there and set it as a current folder. Within it, one could place functions in or have a separate folder for the functions and addpath, as you mentioned.
I have one folder where I have all my math related research. It's on my desktop called "reserach". I extracted the file i downloaded to this folder. So I have to add this folder to some sort of search path, so that when I run the function, the program goes to that folder to see if there is a function called that there?? This is all so confusing, I have very little experience with computers. The help addpath tool tip is even confusing!!
Mario, when I type [m,x] = nsumk(n,k) to the command prompt, I get the following error:
Undefined function or variable 'n'.
You need to either have defined n and k already as variables, or else pass in particular numeric values.
n = 7; k = 4;
[m, x] = nsumk(n, k);
or
[m, x] = nsumk(7, 4);
I put:
>> n = 7; k = 4;
[m, x] = nsumk(n, k);
into the command prompt and get this:
Error: File: nsumk.m Line: 11 Column: 53
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To
construct matrices, use brackets instead of parentheses.
Thx for being patient with me
Weird, I went to that line and put a % at the start of it, now the program runs, Thx

Sign in to comment.

Categories

Asked:

on 18 Sep 2020

Commented:

on 19 Sep 2020

Community Treasure Hunt

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

Start Hunting!