Info
此问题已关闭。 请重新打开它进行编辑或回答。
Create 2 new Columns of data in a table in an existing file after each time the script is run
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Walter Roberson
2019-3-26
编辑:Walter Roberson
2019-3-26
"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?
回答(1 个)
Walter Roberson
2019-3-26
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 个评论
Walter Roberson
2019-3-26
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?
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!