Matlab: variables different formats

In summary, the user is seeking help with setting up different variable formats in MATLAB. However, the format command affects all variables and there is no easy way to assign different formats to specific variables. The user may need to code the format changes or use the fprintf feature with explicit formatting.
  • #1
camimfajardo
2
0
Hi all,

I need some help with matlab. I am trying to set up different variables with different formats. For example: "format long", "format short eng".. etc.

My problems is that when I use the command "format", it assigns the selected format to all the variables. But I want to create a file where some variables have certain format, and some other variables have another format.

Thank you very much,
Regards,
 
Physics news on Phys.org
  • #2
Welcome to PhysicsForums!

Unfortunately, I don't believe that there is a simple way of doing this. Note also that MATLAB's format command only affects the way variables are displayed, not the way they're stored (or treated) internally:
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/format.html

If you want to display certain variables in certain formats, I'd code that in the script you're using to process those in the first place (with format changes everywhere). That or just accept MATLAB's display (and be cognizant of the underlying structure).

Alternately, instead of displaying variables using the variable name, you can make use of the fprintf feature and C-style explicit formatting:
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fprintf.html

Good luck!
 

Related to Matlab: variables different formats

1. What are the different variable formats in Matlab?

In Matlab, there are several variable formats including numeric, character, logical, and structure. Numeric variables can hold numerical values such as integers and decimals. Character variables can hold strings of text. Logical variables can hold true/false values. Structure variables can hold a collection of variables with different formats.

2. How do I create a variable in Matlab?

To create a variable in Matlab, you can use the assignment operator '=' to assign a value to a variable name. For example, if you want to create a numeric variable called 'x' with a value of 5, you can write 'x = 5' in the command window. You can also create variables using functions or by importing data.

3. Can I change the format of a variable in Matlab?

Yes, you can change the format of a variable in Matlab using functions such as 'double', 'char', 'logical', and 'struct'. These functions will convert the variable to the specified format. However, some conversions may result in data loss, so it is important to use caution when changing variable formats.

4. How do I access and manipulate variables in Matlab?

To access and manipulate variables in Matlab, you can use the variable name followed by the dot operator and the desired action. For example, if you want to change the value of a variable named 'x', you can write 'x = x + 1' to add 1 to the current value of 'x'. You can also use functions and logical indexing to manipulate variables.

5. Can I use variables from different formats in the same calculation in Matlab?

Yes, you can use variables from different formats in the same calculation in Matlab. Matlab will automatically convert the variables to a compatible format before performing the calculation. However, it is important to pay attention to the format of the result to avoid unexpected errors or data loss.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
7K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
18
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
22
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
Back
Top