Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations GregLocock on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to write multiple vectors to a single file. 2

Status
Not open for further replies.

konduri

Mechanical
Mar 5, 2004
19
I have four different vectors(column matices) and all of them are different sizes. I want to export all the vectors to a single file. I could write only one vector to a file. Any help is greatly appreciated.

Regards,
Ramesh
 
Replies continue below

Recommended for you

All the file output functions can handle matrices, so you simply need to put your vectors into a single matrix

TTFN
 
IRstuff,

I created a matrix with all the four vectors and tried to output the matrix. But all I can get is the four dashes ( one dash each in 1st cell of four rows). If you can give some examples, I appreciate it.

Regards,
Ramesh
 
what function did you use and how did you use it?

TTFN
 
IRstuff,

I have four column matrices viz., u, v , x and y. They are calculated by set of equations. u and v are same size and x and y are same size.

I used the following:
A:=(u,v,x,y)

I used two different ways to export this matrix to a file.

method 1: writeprn(outfile):=A

method 2: From INSERT menu choose COMPONENT
Select File Write and followed the prompts.

Both methods did not work.

First of all, matrix A just shows that first elements in four colums as column matrix and does not expand.

I was wondering whether there is any append method which I can use.

If you have an answer, please post it.

Thanks
 
It doesn't work because you created a vector with nested vectors, NOT a matrix.

The brute force approach is:
A[i,0:u[i
A[i,1:v[i, etc

Then writeprn(filename):A

TTFN
 
A much easier way would be to use the augment() function.

Say you had column vectors w, x, y, and z, then you can place them side-by-side into an array with the command:

A:augment(w,x,y,z)

(the : symbol places the = assignment operator there)

Then just writeprn(filename):A as before.

You can also use the stack() command to place column vectors end-to-end.

Hope that helps,
Matt
 
Matt,
Thanks for easy step. The one problem I have is that not all column vectors are same size. Is there anyway to augment unequal size vectors? Also is there anymethod to add some comments and variables to the matrix? (Basically, I am trying to create a output file with comments, input variables and output values in a spread sheet.)

Thanks for your help

Ramesh
 
Hi Ramesh,

I think your best bet is to use an Excel Component.

Insert>Component...

Select "Microsoft Excel". Then tell it to make an empty Excel worksheet.

If you have 3 columns vectors (they can be unequal in length), then make 3 inputs and 0 output. Start the first input at A1, the second input at B1, and the third input at C1. Tell it that you want 0 ouputs. And click Finish.

Then in the three placeholders under the Excel worksheet, type the variable names of your column vectors.

The Excel worksheet should contain your data now. :)

You can even double click on the object and directly edit the worksheet just as if you were working directly in Excel. You can also save the worksheet to a file.

Also, it is possible to make an array of strings and specify this as an input to the Excel worksheet object. (At least in Mathcad 11, which is the only version I have access to to test it in.)

Hope that helps! Good luck!
Matt


 
Matt,

I used that in my program. But only problem with that I cannot save the spreadsheet to a file automatically. I need to right click on to export the spreadsheet. I want to make the mathcad program automatic for other users. If I use the component to writeprn, it creates a file with (0,0) cell information in it. I appreciate any other ideas.

Thanks,
Ramesh
 
Ah, that is not a problem :) Here's the beauty of the OLE-embedded Excel object.

Double click on the Excel object. Notice that the menus at the top are suddenly those of Excel and not Mathcad.

Go to Tools>Macro>Visual Basic Editor.

In the project explorer view in the upper left, expand the VBAProject(Worksheet1) project. Highlight "ThisWorkbook" and double click on it.

In the code window that comes up, paste this into it:

Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    ThisWorkbook.SaveAs ("C:\Documents and Settings\Matt\Desktop\test1.xls")
End Sub

You can change what's in the quotes so that it points to the right filename.

Then close out of the visual basic editor and click back into Mathcad to take the focus away from the Excel object to save your changes.

So basically every time the Excel object is changed, the worksheet changes, and this function gets called to save your file.

It might require a tad more tweaking but that is the best I could figure out in 5 minutes.

Hope that helps!
Matt
 
Matt,
Thanks for your spontaneous solution. I implemented your suggestion and it works. But the annoying thing is it asks a confirmation for every variable change in the spread sheet. I have several variables passing to the spreadsheet so I need to click several times to accept the overwrite existing Excel file. I wish there is a way to avoid this.

Ramesh
 
Hi Ramesh,

Yep, you're right, that overwrite confirmation dialog box does get a bit obnoxious after awhile. If you are sure that you'd like to overwrite the file each time, you can use this code instead:

Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    Dim strFileName As String
    strFileName = "C:\Documents and Settings\Matt\Desktop\test1.xls"
    If Application.AlertBeforeOverwriting = True Then
        Application.AlertBeforeOverwriting = False
        ThisWorkbook.SaveAs (strFileName)
        Application.AlertBeforeOverwriting = True
    Else
        ThisWorkbook.SaveAs (strFileName)
    End If
End Sub

It just disables Excel's AlerBeforeOverwriting option if it is set.

Hope that helps!
Matt
 
Matt,
the new program does not work. It still asks to overwrite the existing file. Would you please check the program?

Regards,
Ramesh
 
Hmmm, it works great for me. What version of Excel are you using?

That code that I posted is to be copy/pasted in instead of the previous code you were using. Just wanted to clarify that . I'll take a look at it again.

Matt
 
Matt,
I had Excel 97 and the program did not work. I upgraded Excel to Excel 2000 but it is still not working. Any help is appreciated.

thanks
Ramesh
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor