0

I know that I am pretty confused about arrays and strings and have tried a bunch of things but I am still stumped. I have groups of data that I am pulling into various arrays. For example I have site locations coming from one source. Numerous cores can be at a single location. The cores can have multiple depths. So I am pulling all this data together in various ways and pushing it out into a single excel file for each core. I create a filename based on location id and core name and year the core was sampled. So it might look like ‘ID_14_CORE_Bu-2-MT-1991.xlsx’ and I am storing it to use with a xlswrite statement in a variable called “filename.” This is all working fine. But now I want to keep track of what files I have created and when I created them in another EXCEL file. So I was trying to store the location, filename and the date it was processed into some sort of array so that I can use the xlswrite statement to push it all out after I have processed all the locations/cores/layers that might occur in the original input files.
As I start the program and look in the original input files I can figure out how many cores I have so I wanted to create some sort of array to hold the location, filename and the date together. I have tried to use a cell array (a = cell(numcores,3)) but that does not seem to work. I think I am understanding that the filename is actually a string array so each of the letters is trying to be assigned to a separate cell instead of just the cell in the second column.

I also have had problems trying to push the three values out to the summary EXCEL file as each core is being processed but MATLAB tends to treat single dimensional arrays as a row rather than a column so I am kind of confused there.

Below is what I want an array to end up like…but since I am developing the filename on the fly this seems to be more challenging.

ArraytoExcel = [“14”, “ID_14_CORE_Bu-2-MT-1991.xlsx”,”1/1/2018”;
“14”, “ID_14_CORE_Bu-3-MT-1991.xlsx”,”1/1/2018”;
“13”, “ID_13_CORE_Tail_33-1992.xlsx”,”1/1/2018”;]

Maybe I am just going about this the wrong way. Any suggestions would help.

5
  • Change your square brackets [ ] to curly brackets { } to get a cell array. It would also be a good idea to turn off smart quotes when pasting code. Commented Feb 6, 2018 at 19:13
  • I think you missed the point. The example above is what I want the array to actually end up like. It is not an array that is being read in as a whole from one source and it is not a static array within the program. Commented Feb 6, 2018 at 19:16
  • Yes, that's entirely possible. I have to admit my eyes glazed over a bit reading through the question. Commented Feb 6, 2018 at 19:19
  • @user1968084 can you please shorten the question text? I'm afraid it's a bit too much context. Commented Feb 6, 2018 at 19:22
  • Basically I have a variable that is created on a fly that is a string. And I have another variable that is associated with the first variable. These variables are in a loop. I want to create some sort of array to hold these two variables for each pass of the loop. After the lopp is finished I want to push the array out into EXCEL that will only fill two columns and the number of rows that is the number of passes through the loop. Commented Feb 6, 2018 at 19:30

1 Answer 1

1

Your question is a little confusing but I think you want to do something like the following. The variables inside of my example are static but from your question it sounds like you already have these figured out somehow.

numcores = 5;  %.. Or however, you determine what you are procesing
ArraytoExcel = cell(numcores ,3);
for ii = 1:numcores      
    %These 3 things will need to determined by you in the loop
    % and not be static like in this example.
    coreID   = '14';    
    filename = 'ID_14_CORE_Bu-2-MT-1991.xlsx'; %
    dataProc =  datestr(now,'mm/dd/yyyy');    
    ArraytoExcel(ii,:) = {coreID,filename,dataProc};
end
xlswrite('YourOutput.xls',ArraytoExcel)
Sign up to request clarification or add additional context in comments.

1 Comment

BINGO!!!!!!!!!!!!!!!!!! That was the problem. I needed to assign the variables to the cell array with the braces and also not as individual pieces. Thank you so much

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.