LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Some thing in my code causing excel file to be READ-ONLY

Hello:
 
I am pretty certain problem is caused by left bottom part of the picture, but I am not sure why.  The software basically write a header to a .xls file at start called XXX-peak.xls, at every cycle I read everything from the .xls, do some array manipulation modify 2 columns to the last row and rewrite the whole thing back to the same file.  I originally thought perhaps I left the file unclosed after an access but I think that would cause an error, besides,  all the read/write file VIs have file close features inside of them.  If someone could give me a quick diagnostic, it would be great!
 
JC  
0 Kudos
Message 1 of 10
(3,060 Views)
Yes, the problem is in the lower left corner. The call to "Open/Create/Replace File.vi" creates an open file refnum in the upper right terminal. If you open a file, you must close it before opening it again. The best way to do what you want to do is to get rid of the "Open/Create/Replace File.vi" and simply wire your file path to "Write Characters To File.vi". This latter function opens and closes the file each time it is called. Open it up to see for yourself.

Jayme
jc
Mac 10.4
LV7.1
CLD
0 Kudos
Message 2 of 10
(3,049 Views)

First of all, you're not reading or writing any files in Excel (*.xls) format. You're reading and writing plain ASCII spreadsheet data. (The fact that Excel will try its best to parse the content when you later try to open it with excel does not make it an Excel file ;)).

It is not obvious from the picture where the problem is, especially since we don't see the code in all the other cases and you read your filename via a value property. Where else is the file name used? It might be more efficient to open the file once, then use low-level file I/O to read from it and write back to it at desired offset location without ever closing the file during the run of the program.

Why don't you attach the actual VI so we can better see what you're doing?

0 Kudos
Message 3 of 10
(3,046 Views)
Ok yeah I see the problem guys, I originally copied that bottom left side into an untitled vi, did not cause the excel file to become READ ONLY.  But then I put a for loop out side of it, the excel file soon became READ ONLY.  I chose the "open/create/replace file.vi" so that  I will not have to click on the dialog box everytime I  overwrite a file,  "writechacterstofile" gives me the option of append or replace but dialog occurs everytimes when I want to overwrite, which occurs every cycle (about 5 sec).  I am trying to put a close file to the refnum, see if that works.
 
I agree, writing arrays to a spreadsheet string, then wrting string to a file does not mean it is a excel file.  I have to use active x for that.
0 Kudos
Message 4 of 10
(3,039 Views)
Make sure that the file is closed before you call "Write Characters To File.vi" and it should work.
jc
Mac 10.4
LV7.1
CLD
0 Kudos
Message 5 of 10
(3,031 Views)
Yeah, that fixed it, I kept everything at the lower bottom corner, I just added a close file and wired the refnum over.  Thanks.
0 Kudos
Message 6 of 10
(3,032 Views)
In your fix, you describe a situation in which a race condition is possible, i.e. the "Write Characters To File.vi" function could execute before the "Close File.vi" function. In this case, the "Write Characters To File.vi" would fail to write the new data. This may never happen for you, but if you don't explicitly force the order of execution for these two vis to be the order you want, you may find yourself making an edit at a later time that may cause your previous error to come back. If you wire the path out of the "Close File.vi" function into the input path of the "Write Characters To File.vi", you will force the order of execution with the proper data dependency.
jc
Mac 10.4
LV7.1
CLD
0 Kudos
Message 7 of 10
(3,027 Views)
After a closer look, I can see that there is some redundancy in your code. You first read the entire file and format it into a spreadsheet string. You then extract the last row of the spreadsheet and replace the last entry (7th) in the row before tacking it back on to the spreadsheet. You are manually appending your data. You then attempt to append with the "Write Characters To File.vi". Something looks fishy.
jc
Mac 10.4
LV7.1
CLD
0 Kudos
Message 8 of 10
(3,023 Views)
After a closer look, I can see that there is some redundancy in your code. You first read the entire file and format it into a spreadsheet string. You then extract the last row of the spreadsheet and replace the last entry (7th) in the row before tacking it back on to the spreadsheet. You are manually appending your data. You then attempt to append with the "Write Characters To File.vi". Something looks fishy.
 
The file has a header and some data, the purpose of the code is to input data at 2 column positions in the last row of the file EVERY cycle.  Now with "WriteCharactersToFile.vi"  I have two options, 1-append and 2-replace.  I can not append my new colum data to a NON-empty file because I need my data to be on the last row, append just gives me a new row.  So to do this, I am reading in the whole thing  and convert spreadsheet string into array so I can replace the last row, then I convert the whole array back to spreadsheet string then write to a blank file, which allows me to append without causing a pop up dialog in mid cycle.
 
This is not the best way I am sure, if u have some input let me know.  I wired output path to the input of "WriteCharactersToFile.vi", you were correct about possible race condition.  I just was not sure if I could write to a file after I have closed it, maybe closing the reference is different?
0 Kudos
Message 9 of 10
(3,018 Views)
There are many ways to log data. Because of the myriad approaches available, it is important to have a more detailed explanation of the problem, usually in the form of your code. If you attached your code, you would probably get a couple of responses that would be an improvement over your current code and you would experience a tutorial-type lesson in coding by example. You might be surprised how much time people will spend working your problem just to show that they have the skill to answer your question.

The function you use to append to your file opens and writes and closes. You effectively open a new file (create) with the Open/Create/Replace, and then close it. Now you have an existing file with the name in your path wire. You then open-write-close. You could rewrite this so that you wouldn't have to perform two opens and closes.

If you are going to replace or create, use new in the advanced file functions palette, which silently creates or overwrites, then use write and close. If you are going to append, use the "Write Characters To File.vi"

If what you have is working, I recommend we move on.

Good luck LabVIEWing.

Jayme
jc
Mac 10.4
LV7.1
CLD
0 Kudos
Message 10 of 10
(3,012 Views)