Info

This question is closed. Reopen it to edit or answer.

Create 2 new Columns of data in a table in an existing file after each time the script is run

1 view (last 30 days)
for i = 1:10
someinfo = readtable("someinfo.csv");
[q, p] = getqp(someinfo);
table.column1 = p;
table.column2 = q*p
end
I have a matlab script that generates a csv file with data but its overwriting data in column1 and column2. I wanted data in newcolumns
Everytime I run my script, I want 2 new columns created beside column1 and 2. The new columns are getting information from a database with changing information however the information is assigned to p and q so the formulas for the first and second new columns I wish to be generated are exactly the same as column 1 and 2 respectively.
table.columnm = p; table.columnn = q*p;
I want these new columns to be added to the existing csv file and have a new name based on each run.
What is confusing me is the logic. I don't understand where in my code I would implement this feature. I know it wouldnt be in this for loop
  2 Comments
Walter Roberson
Walter Roberson on 26 Mar 2019
Edited: Walter Roberson on 26 Mar 2019
"and have a new name based on each run"
What name should they have? "column" followed by the next available column number?
Would "var" followed by the next available column number be acceptable?
N/A
N/A on 26 Mar 2019
Edited: N/A on 26 Mar 2019
Would "var" followed by the next available column number be acceptable? Yes
What name should they have? "column" followed by the next available column number? Yes, this is fine too
I just want new data to append to the existing file instead of overwriting it every time the script is run

Answers (1)

Walter Roberson
Walter Roberson on 26 Mar 2019
new_table{:,end+1} = p;
new_table{:end+1} = p*q;
Question: is p a vector? Is q a scalar or a vector or an array? I am concerned about whether p*q invokes algebraic matrix multiplication and whether the result is a vector of the correct size. Is it possible for some results to be a different size than others?
  5 Comments
Walter Roberson
Walter Roberson on 26 Mar 2019
Notice the when you use a single output for size() then the result is always a vector with minimum length 2. You then use that vector as the upper bound in for i=1:rows rather than having the bound be a scalar. MATLAB does define the operation: it ignores anything beyond the first element, so the call becomes effectively for i=1:rows(1) ... but along the way you would have managed to confuse people reading the code about whether you were doing this deliberately or accidentally. Perhaps you should use rows = size(List,1)
If List is a table object then you could also use rows = height(List)
Your code can only work if List is either a table object or a nonscalar struct (or it is some kind of object).
You write into the structure cardList but you never save the structure anywhere.
What you writetable() is the original List input, unchanged.
You return url, Id, rarity from the function, but their values are going to be whatever they were left at in the loop -- so they will happen to correspond to the information about the last row of List. Is that useful and meaningful, that you bother to return those for exactly the last row?
N/A
N/A on 26 Mar 2019
The output was supposed to be Name, Id, and rarity, sorry.
Let me preface this by saying there is more code and the script is working.
You are absolutely correct in your observations. Everything is going exactly as planned. It is all deliberate.
The intention for name id and rarity to be outputting from the function is to do exactly what you just stated, to return the information pertinent and then loop back and do the same for the next name.
By having the outputs as those listed, I will be able to add a search feature later on to scour through the data and return the information I request. But thats later.
So now the question comes again
How do I do this such that it appends instead of overwrites?

Tags

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!