Writing data to readable workspace table

3 Ansichten (letzte 30 Tage)
Jason
Jason am 7 Mai 2023
Kommentiert: Stephen23 am 5 Jun. 2023
I apologise, i am sure this is trivial but have struggled for a few hours with this.
I simply want to define some variables and constants, generate an associated table that is both saved to my hard drive and appears in a readable manner in my workspace. I have been using the "writetable" command but this is not doing what i want. Sure i can generate txt, xls files likes this but they are all single strings of numbers that cannot be read back in
A very simple example:
x=[0:0.1:10]
y=exp(x)
T=table(x,y)
writetable(T, 'jjdexp.txt')
load 'jjdexp.txt' - this line fails - can't read table as written
Basically , for two variables, for example, i want to generate a readable/loadable 2x1 table on my harddrive and workspace
any advice hugely welcome
Jason
  2 Kommentare
Peter Perkins
Peter Perkins am 5 Jun. 2023
Jason, I doubt that this
x=[0:0.1:10];
y=exp(x);
T=table(x,y)
T = 1×2 table
x y ____________ ____________ 1×101 double 1×101 double
is the table you want. I imagine you want
x=[0:0.1:10]';
y=exp(x);
T=table(x,y)
T = 101×2 table
x y ___ ______ 0 1 0.1 1.1052 0.2 1.2214 0.3 1.3499 0.4 1.4918 0.5 1.6487 0.6 1.8221 0.7 2.0138 0.8 2.2255 0.9 2.4596 1 2.7183 1.1 3.0042 1.2 3.3201 1.3 3.6693 1.4 4.0552 1.5 4.4817
Tables are column-oriented, but they are able to hold variables that themselves have multiple columns.
Stephen23
Stephen23 am 5 Jun. 2023
And this:
x = [0:0.1:10];
is simply written like this, without the superfluous concatenation operator:
x = 0:0.1:10;

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Cris LaPierre
Cris LaPierre am 7 Mai 2023
If you save your table using writetable, then you should probably read it back in using readtable.
T = readtable('jjdexp.txt');
The load command is more commonly used with mat and ascii files. See here.
Of course, this is completely unnecessary so long as variable T is still in your workspace.
  2 Kommentare
Jason
Jason am 7 Mai 2023
Verschoben: Walter Roberson am 7 Mai 2023
much appreciated and apologies for not seeing this
Walter Roberson
Walter Roberson am 7 Mai 2023
When you load() a text file, and MATLAB is able to recognize it as a text file, then officially that form of load only supports files saved with save -ascii .
In practice, what load() of a text file supports is:
  • empty lines are ignored (so there may be leading empty lines)
  • If the line contains a % anywhere on the line, then from the % to the end of the line is ignored. For example 1% markup would be considered a valid entry of the number 1 with the % onward being ignored
  • other than in comments, only number-forming-characters and comma and semi-colon are permitted. Quoted text is not permitted. Text (such as headers) is only permitted after % (that is, as a comment)
  • commas and semi-colons may be used to separate columns
  • there must be the same number of (not-commented) numeric columns on each line
  • The letter designating a exponent may be e E d D
  • Complex numbers immediately following another number with no space will be permitted but will be ignored. For example 1-3.7i will be treated as 1 with no error. i and j are both accepted for complex component indicator
  • imaginary-only components will have the i or j immediately beside them, will be accepted but treated as real values. So for example 1 -3.7i 8.9 will be accepted as [1, -3.7, 8.9]
When you writetable() with the default options, variable names are emitted on the first line of the resulting output. Those variable names will very likely not happen to start with '%' and not happen to look like numbers (you can deliberately create tables with variables to get around this, but it would seldom happen naturally.) That row of variable names would then violate the rules about only-comments-and-numbers-and-delimiters and so not be accepted by load()

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Tables finden Sie in Help Center und File Exchange

Produkte


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by