Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


MATLAB - basic questions

Posted on 2004-10-29
Medium Priority
Last Modified: 2012-06-27
Hi all,

1) How do I print a formatted string to screen? sprintf also prints "ans = ...". fprintf works perfectly well, but this doesn't seem to be the most elegant way, because this is used for writing to files (and I cannot specify print to stdout, like in C).

2) How do I pass parameters by reference to a function?

3) When I write "load f1.txt", "load f2.txt" etc, the matrixes are
loaded into f1, f2 etc. Can I load it to a certain variable NOT with the name of the file? (I want it to be loaded to "f", for example).Of course I can read into f1, and then just copy into f, and then clear f1, but I have hundreds of files, and this will take extra time.
Something else that could solve this is to define a pointer. I.e. load the file into f1, and then assign f to be a pointer to f. I prefer the first way (difect reading to "f"), but if it's impossible, a pointer will also do...

Please help.
Question by:slavikn
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

rjkimble earned 465 total points
ID: 12453425
I don't have access to MATLAB, so I'm using Octave, which is supposed to be a fairly good clone. Its documentation is fairly weak, however.

For (1), you could try wrapping the sprintf output using disp: disp(sprintf(....)). That seems to work OK for me.

Octave documentation states that all function calls are by value, and that you cannot call a function by reference. However, in googling around a bit, I stumbled onto this page: http://www.me.pdx.edu/~gerry/MATLAB/programming/basics.html. On it, there is this warning in bold letters: "Unlike other languages, the variables in the input_parameter_list should never be altered by the statements inside the function." That suggests to me that MATLAB passes arguments by reference.

For (3), Octave also has a load function, and it seems to work the same way that you describe. If you insert appropriate comments at the beginning of the file, you can control the name of the variable to which the data is assigned. Here's an example:

# name: m
# type: matrix
# rows: 1
# columns: 5
 1 0 1 0 1

When you load such a file, the array data will be assigned to the variable, "m." Perhaps you can add such comments when you create your files.

Hope this helps. Maybe somebody who actually uses MATLAB can provide some better answers.

Author Comment

ID: 12477368

1) Indeed, disp(sprintf(...)) works. Thanks.
2) No. Variables aren't passed by reference.
3) I've found a way: whatever = load('filename');

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Complex Numbers are funny things.  Many people have a basic understanding of them, some a more advanced.  The confusion usually arises when that pesky i (or j for Electrical Engineers) appears and understanding the meaning of a square root of a nega…
Lithium-ion batteries area cornerstone of today's portable electronic devices, and even though they are relied upon heavily, their chemistry and origin are not of common knowledge. This article is about a device on which every smartphone, laptop, an…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question